【发布时间】:2019-01-11 23:13:46
【问题描述】:
Flow 的 dynamic code example 表示 Flow 可以找出运行时类型检查:
function foo(x) {
if (typeof x === 'string') {
return x.length; // flow is smart enough to see this is safe
} else {
return x;
}
}
var res = foo('Hello') + foo(42);
但在现实生活中,typeof 还不够好。我通常使用 lodash 的类型检查函数(_.isFunction、_.isString 等),它们可以处理很多边缘情况。
问题是,如果我们将示例更改为使用 lodash 进行运行时类型检查,Flow 将不再理解它:
function foo(x) {
if (_.isString(x)) {
return x.length; // warning: `length` property not found in Number
} else {
return x;
}
}
var res = foo('Hello') + foo(42);
我尝试使用iflow-lodash,但在这里似乎没有什么不同。
让 Flow 理解使用 lodash 进行运行时类型检查的代码的最佳解决方案是什么?顺便说一句,我是 Flow 新手。
【问题讨论】:
标签: flowtype