所以我使用了React,我有以下代码:
type Props = {
children: SuperOption | HTMLHrElement | Array<SuperOption | HtmlHrElement>,
}
然后在我的代码中,我有:
let arr: Array<SuperOption | HtmlHrElement>;
if (this.props.children.length) {
arr = this.props.children;
} else {
arr = [this.props.children];
}
这给了我一个流错误:
HTMLHRElement This type is incompatible with array type
SuperOption This type is incompatible with array type
我认为这是因为flow不知道我试图通过执行this.props.children.length
将其细化到数组,那么将其细化为数组类型的正确方法是什么呢?
发布于 2018-01-11 06:45:07
优化数组的正确方法是使用Array.isArray
函数:
if (Array.isArray(this.props.children)) {
arr = this.props.children;
} else {
arr = [this.props.children];
}
https://stackoverflow.com/questions/48193052
复制相似问题