我有这个非常简单的功能,我试图用Flow验证:
brandId
问题是我收到了这个错误:
// @flow
type Props = {
width: string | number,
};
function fun({
width = '30em',
}: Props) {
return width;
}
我想知道我做错了什么......另一种方式很好:
8: width = '30em',
^ number. This type is incompatible with
8: width = '30em',
^ string
8: width = '30em',
^ string. This type is incompatible with
8: width = '30em',
^ number
8: width = '30em',
^ string. This type is incompatible with
8: width = '30em',
^ number
支持函数参数中的这种语法,因为:
// @flow
type Props = {
width: string | number,
};
function fun(props: Props) {
const {
width = '30em',
} = props;
return width;
}
这很好用。
想法?
答案 0 :(得分:0)
Destructuring default assignment is not supported in Flow.
您应该单独定义defaultProps,然后将它们用作默认值,而不是同时分配默认值和解构。
例如:
type Props = {
width: string | number
}
const defaultProps: Props = {
width: '30em',
}
function fun ({ width }: Props = defaultProps) {
return width
}
fun({ width: 30 }) // => 30
fun({ width: '30' }) // => '30'
fun() // => '30em'
fun({ width: true }) // $ExpectError
答案 1 :(得分:0)