Nullity guard 不适用于 forEach 循环内部
Nullity guard doesn't work for inside forEach loop
在下面的代码中,第二个守卫不能在 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.
}
在下面的代码中,第二个守卫不能在 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.
}