As stated in TypeScript 3.7 reference on optional chaining, we can secure access to properties or methods to possibly null
or undefined
instances with the ?.
notation.
Under TypeScript 4.9.4, tsconfig.json
(target: "ES2020"
& module: "commonjs"
), Visual Studio Code, different situations lead to pre-transpilation errors: "Property xxxxx does not exist on type never ".
let a
let a: string | null = null
let a: string | undefined = undefined
console.log( a?.toString() ) // << "Property toString does not exist on type never "
The only way I had identified it worked is in case of intermediary of function or lambda (arrow function) interfaces:
let lambda = (value: string | null) => {
console.log(value?.toString()) // << no complaining
}
let a = null
lambda(a) // << no complaining
a = "Hello World!"
lambda(a)
在该案中,预审人员没有投诉。
为什么它不像文件所说的那样工作?