Ell*_*ski 5 variables initialization undefined let typescript
我想了解以下 TypeScript 行为:
下面的代码
let a: number
if (a === undefined) {
console.log("how?")
}
Run Code Online (Sandbox Code Playgroud)
抛出错误:“变量'a'在分配之前被使用。”。
但是下面的代码
let a: number
const f = (): void => {
if (a === undefined) {
console.log("how?")
}
}
f()
Run Code Online (Sandbox Code Playgroud)
工作正常并记录“如何?”。
这是为什么?而且,a === undefined
如果它的类型是 ,那又如何呢number
?