启用strictNullChecks
时,以下代码在TypeScript中产生错误:
function foo(cb) {
cb()
}
let h: { a: number } | undefined = undefined // required to reproduce this bug
foo(() => h = { a: 1 })
if (h) {
console.log(h.a) // <-- h is of type `never`
}
答案 0 :(得分:0)
这是TypeScript控件流分析的设计限制。
参考: