属性 无效性检查在迭代中不起作用

Property nullity check doesn't work on iteration

在下面的代码中,ab.bv 检查无法迭代 ab.bv。但是,它可以访问 ab.bv 的元素。 if (ab.bv) 检查是否未检查 ab.bv 的无效性?

type B = {|
    bv: ?B[], 
|}

const c: B => void = n => {}

let ab: B = {bv: []}
if (ab.bv) {
  for (const v2 in ab.bv) { // Error in ab.bv: cannot iterate using a `for...in` statement because array type [1] is not an object, null, or undefined. [invalid-in-rhs]
    c({...ab})
  }
  c({...ab.bv[0]}) // Works
}

细化成功,只是错误有点不清楚。 Flow 试图告诉您应该使用 for...of 而不是 for...in 来迭代数组(因为您将获得索引而不是数组项):

let ab: B = {bv: []}
if (ab.bv) {
  for (const v2 of ab.bv) { // Works
    c(v2)
  }
  c({...ab.bv[0]}) // Works
}

Try