Javascript Flowtype未检查导入的redux容器上的道具
我在一个redux项目上遇到了一个令人困惑的行为测试flowtype(0.43.0),它使用来自flow-TypedRepo的react-redux类型Javascript Flowtype未检查导入的redux容器上的道具,javascript,reactjs,react-redux,flowtype,Javascript,Reactjs,React Redux,Flowtype,我在一个redux项目上遇到了一个令人困惑的行为测试flowtype(0.43.0),它使用来自flow-TypedRepo的react-redux类型 // @flow import React from 'react' import {connect} from 'react-redux' class ChildComponent extends React.Component { props: { value: string, otherValue: string
// @flow
import React from 'react'
import {connect} from 'react-redux'
class ChildComponent extends React.Component {
props: {
value: string,
otherValue: string
}
render() {
return <span>{ this.props.value && this.props.otherValue }</span>
}
}
function mapStateToProps(state) {
return {otherValue: "test"}
}
const ConnectedChildComponent = connect(mapStateToProps)(ChildComponent)
class ParentComponent extends React.Component {
render() {
return <ConnectedChildComponent/>
}
}
/@flow
从“React”导入React
从“react redux”导入{connect}
类ChildComponent扩展了React.Component{
道具:{
值:字符串,
其他值:字符串
}
render(){
返回{this.props.value&&this.props.otherValue}
}
}
函数MapStateTops(状态){
返回{otherValue:“test”}
}
const ConnectedChildComponent=connect(mapStateToProps)(ChildComponent)
类ParentComponent扩展了React.Component{
render(){
返回
}
}
上面的代码按预期执行类型推断,并报告父级中的用法错误,指示应设置“value”属性
但是,当我将容器子代码移动到一个单独的文件中时,我发现没有进行类型检查(可能是隐式的“any”导入)
//flowTestPatent.js
//@flow
从“React”导入React
从“react redux”导入{connect}
从“./flowTestChild”导入ConnectedChildComponent
类ParentComponent扩展了React.Component{
render(){
返回
}
}
//flowTestChild.js
//@flow
从“React”导入React
从“react redux”导入{connect}
导出类ChildComponent扩展了React.Component{
道具:{
值:字符串,
其他值:字符串
}
render(){
返回{this.props.value&&this.props.otherValue}
}
}
函数MapStateTops(状态){
返回{otherValue:“test”}
}
导出默认连接(MapStateTops)(子组件)
当上面的代码看起来应该与第一个示例等效时,它不会报告任何错误。我会假设我在某种程度上执行了错误的导入,但是当我导入未连接的组件时,类型检查会按预期进行。有人能解释一下这里发生了什么吗?谢谢 我不知道为什么把它放在一个文件和单独的文件之间有区别。但是,如果您在redux repo中查看了,他们在处理容器的输入时确实有点不同 基本上,您最终手动指定连接器的类型,这是connect返回的函数。不幸的是,在流中键入HOC需要这种手动步骤,因为 所以在你的情况下,你会
// @flow
import React, { Component } from 'react'
import { connect, type Connector } from 'react-redux'
type Props = {
value: string,
otherValue: string,
}
type OwnProps = {
value: string,
}
class ChildComponent extends Component {
props: Props
render() {
return <span>{ this.props.value && this.props.otherValue }</span>
}
}
function mapStateToProps() {
return {
otherValue: 'test',
}
}
const connector: Connector<OwnProps, Props> = connect(mapStateToProps)
const ConnectedChildComponent = connector(ChildComponent)
class ParentComponent extends Component {
render() {
return <ConnectedChildComponent />
}
}
/@flow
从“React”导入React,{Component}
从“react-redux”导入{connect,键入连接器}
类型道具={
值:字符串,
其他值:字符串,
}
类型OwnProps={
值:字符串,
}
类ChildComponent扩展组件{
道具:道具
render(){
返回{this.props.value&&this.props.otherValue}
}
}
函数mapStateToProps(){
返回{
otherValue:'测试',
}
}
常量连接器:连接器=连接(mapStateToProps)
const ConnectedChildComponent=连接器(ChildComponent)
类ParentComponent扩展组件{
render(){
返回
}
}
使用上面的代码,我得到一个错误
26: const connector: Connector<OwnProps, Props> = connect(mapStateToProps)
^^^^^^^^ property `value`. Property not found in
31: return <ConnectedChildComponent />
^^^^^^^^^^^^^^^^^^^^^^^^^^^ props of React element `ConnectedChildComponent`
26:const connector:connector=connect(mapStateToProps)
^^^^^^^^属性“值”。在中找不到属性
31:返回
^^^^^^^^^^^^^^^^^^^^^^^^^^^React元素`ConnectedChildComponent的道具`
你的例子不适合我。即使在同一个文件中,我现在也没有对所连接组件的用法进行类型检查。我正在从'react redux'导入“Connector”类型为导入类型{Connector},并且flow报告代码是正确的。已更新以包含所有代码。按原样,它将有一个流错误。将value prop添加到ConnectedChildComponent,错误即得到解决。
26: const connector: Connector<OwnProps, Props> = connect(mapStateToProps)
^^^^^^^^ property `value`. Property not found in
31: return <ConnectedChildComponent />
^^^^^^^^^^^^^^^^^^^^^^^^^^^ props of React element `ConnectedChildComponent`