我有一个使用Flow输入的React组件:
import { isFunction } from './utils';
type Props = {
...
onRemove?: Function,
};
class MyComponent extends React.Component<Props> {
render() {
// ...
}
_handleRemove = () => {
if (isFunction(this.props.onRemove)) {
this.props.onRemove();
}
}
}
这是我的isFunction
工具:
export function isFunction(value: any): boolean %checks {
return typeof value === 'function';
}
检查我的代码时Flow引发以下错误:
Cannot call onRemove because undefined [1] is not a function.
要么忽略util函数,要么简单地使用typeof this.props.onRemove === 'function'
或将函数定义移到组件文件中即可解决该错误,但是我显然希望能够在我的整个代码库中使用util函数。
我做错了什么吗?或者使用进口产品时Flow是否有问题?如果是后者,是否有已知的解决方法?