在Flow中细化未初始化的变量

时间:2017-06-09 00:46:15

标签: javascript initialization flowtype

如何正确对?TT|undefined类型的未初始化变量进行细化?

是什么让uninitialized variableundefined的值不同?

不会抛出错误,但在下面的情况下:

const test = (nums: Array<number>) => {
  let lastNumber: ?number
  const output = []
  if (lastNumber != null) {
    output.push(lastNumber /*lastNumber is Uncovered code*/)
  }
  return output
}

flow type-at-pos进入lastNumber(unknown),因为uninitialized variable(在声明时)

但这完全覆盖了:

const test = (nums: Array<number>) => {
  let lastNumber: ?number = undefined
  const output = []
  if (lastNumber != null) {
    output.push(lastNumber /*Fully covered*/)
  }
  return output
}

0 个答案:

没有答案