reactjs - 使用导入的函数时,流不会推断类型细化
问题描述
我有一个使用 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 在使用导入时有问题?如果是后者,是否有已知的解决方法?
解决方案
推荐阅读
- c# - 查找字段值不在列表中的记录
- amazon-web-services - 用于监控 IoT 的 AWS IoT 扩展问题和指标
- flutter - 在 flutter_driver 测试期间图像不可见
- ubuntu - Nginx 地址 80 端口已被使用
- amazon-web-services - 如何将生命周期规则发送到 terraform 中的 s3 模块
- python - 如何在没有本地配置的 EC2 实例上使用 boto3?
- symfony - 找不到 Codeception/Symfony4 环境变量:“DATABASE_URL”
- php - 在 phpmyadmin 中执行 mysql 脚本时,如何区分本地开发数据库和实时生产数据库?
- python - 如何替换 tkinter 中的文本?
- javascript - 如何设置控制器,以便根据选择的按钮执行特定命令?