【发布时间】:2018-10-31 16:34:05
【问题描述】:
我一直在尝试将功能性 React 组件从 PropTypes 转换为 Flow。该组件被赋予一个对象,该对象包含要在该组件中使用的字符串。
以前看起来是这样的:
Component.propTypes = {
strings: PropTypes.objectOf(PropTypes.string),
}
Component.defaultProps = {
strings: {
key_one: "string one",
key_two: "string two",
}
}
我已将其转换为:
type Props = {
strings?: { [string_key: string]: string },
}
const Component = ({ strings }: Props) => (
// component using
// label={strings.key_one}
// etc
)
Component.defaultProps = {
strings: {
key_one: "string one",
key_two: "string two",
}
}
这会产生错误:
Cannot get strings.key_one because property key_one is missing in undefined [1].
[1] strings?: { [string_key: string]: string },
如果我删除[string_key: string]: ?string,除了第一条错误消息之外,它还会产生第二条错误消息:
property key_one is missing in object type [1].
如果我删除可选标记,那么错误就会消失,但我无法提供默认道具 (strings?: { [string_key: string]: string }, -> strings: { [string_key: string]: string },)。
阅读问题似乎可能是由于 refinement invalidation 发生在 JSX 的转换中,此处注明:https://github.com/facebook/flow/issues/6350。
任何人都可以进一步阐明,并知道是否有办法解决这个问题?
【问题讨论】: