【发布时间】:2017-08-30 09:50:42
【问题描述】:
我遇到了一个令人困惑的行为,在 redux 项目中使用流类型存储库中的 react-redux 类型测试 flowtype(0.43.0)。
// @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/>
}
}
以上代码按预期进行了类型推断,在parent中使用报错,提示应该设置“value”prop。
但是,当我将容器子代码移动到单独的文件中时,我发现类型检查没有发生(可能是隐式的“任何”导入)
// flowTestPatent.js
// @flow
import React from 'react'
import {connect} from 'react-redux'
import ConnectedChildComponent from './flowTestChild'
class ParentComponent extends React.Component {
render() {
return <ConnectedChildComponent/>
}
}
//flowTestChild.js
// @flow
import React from 'react'
import {connect} from 'react-redux'
export 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"}
}
export default connect(mapStateToProps)(ChildComponent)
上面的代码看起来应该与第一个示例等效时没有报告任何错误。我会假设我以某种方式错误地执行了导入,但是当我导入未连接的组件时,类型检查会按预期进行。任何人都可以帮助解释这里发生了什么吗?谢谢!
【问题讨论】:
标签: javascript reactjs react-redux flowtype