【发布时间】:2019-02-23 19:31:17
【问题描述】:
给定以下伪 React 组件:
type Props = {
width?: number | string
};
const Component = ({
width = '100%'
}: Props) => ( /* component body */ )
Flow 抛出以下错误:
6: width = '100%'
^ string [1] is incompatible with number [2].
References:
2: width?: number | string
^ [1]
2: width?: number | string
^ [2]
6: width = '100%'
^ string [1] is incompatible with number [2].
References:
6: width = '100%'
^ [1]
2: width?: number | string
^ [2]
组件应接受width 的3 种可能类型:void | number | string。有正确的输入方法吗?我可以通过删除 '100%' 的默认值来修复该错误,但该解决方案不太惯用。
【问题讨论】:
标签: javascript reactjs ecmascript-6 flowtype destructuring