如何正确对?T
或T|undefined
类型的未初始化变量进行细化?
是什么让uninitialized variable
与undefined
的值不同?
不会抛出错误,但在下面的情况下:
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
}