当打开 —strictNullChecks 配置:当你声明任何一个类型,都默认不包括 nullundefined,但你可以明确声明一个联合类型:

    1. let exampleString = "foo";
    2. exampleString = null;
    3. // Type 'null' is not assignable to type 'string'.
    4. let stringOrNull: string | null = "bar";
    5. stringOrNull = null;
    6. stringOrNull = undefined;
    7. // Type 'undefined' is not assignable to type 'string | null'.

    可选链:

    1. let x = foo?.bar.baz();
    2. // 等价于
    3. let x = foo === null || foo === undefined ? undefined : foo.bar.baz();

    非空断言:
    语雀内容