X |如果不检查箭头功能,不确定的范围将缩小到永远

时间:2018-10-22 17:48:32

标签: typescript

启用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`
}

TypeScript Playground

1 个答案:

答案 0 :(得分:0)

这是TypeScript控件流分析的设计限制。

参考:

takes a single argument

https://github.com/Microsoft/TypeScript/issues/28038