Typescript 3.7.2新特性

新特性

可选链(Optional Chaining)

let x = foo?.bar.baz();

等效于:

let x = (foo === null || foo === undefined) ?
    undefined :
    foo.bar.baz();

注意:当bazundefined时,仍然会报错,这时需要再次判断baz是否为空:

let x = foo?.bar?.baz?.call(null)
// 这里我使用了call来调用方法

Last updated