【发布时间】:2020-12-15 12:52:27
【问题描述】:
在下面的代码中,第二个守卫不能在forEach 循环内断言ab.cv 的非空性,其中取消注释本地相同的守卫使其工作。为什么会这样?
type B = {|
bv: B[],
cv: ?number
|}
let ab: B = {bv: [], cv: 1}
if (ab.cv) { // First guard
const a = ab.cv + 1 // Works
}
var a2;
if (ab.cv) { // Second guard
ab.bv.forEach(b => {/*if (ab.cv) */a2 = ab.cv + 1}) // Doesn't work except if uncommented
// Cannot perform arithmetic operation because null or undefined [1] is not a number.
}
【问题讨论】:
标签: javascript functional-programming flowtype