Optional Chaining 及 Nullish Coalescing 可以大大提升 Source Code 可讀性,冇呢兩個 Features 要寫
Equivalent 嘅 Implementation 其實好難。
interface Foo {
bar?: Bar
}
interface Bar {
baz(): string
}
function getFooOrUndefined() : Foo | undefined {
return undefined
}
const foo: Foo | undefined = getFooOrUndefined()
const omg = foo?.bar?.baz() ?? 'omg'
阿 Gap 將上述 Typescript Compile 變做 Javascript 比大家作一個參考:
"use strict";
var _a, _b;
function getFooOrUndefined() {
return undefined;
}
const foo = getFooOrUnd