TypeScript 4.4

TypeScript 4.4

针对条件表达式和判别式的别名引用进行控制流分析

在 JavaScript 中,总会用多种方式对某个值进行检查,然后根据不同类型的值执行不同的操作。 TypeScript 能够理解这些检查,并将它们称作为类型守卫。 我们不需要在变量的每一个使用位置上都指明类型,TypeScript 的类型检查器能够利用基于控制流的分析技术来检查是否在前面使用了类型守卫。

例如,可以这样写

```ts twoslash function foo(arg: unknown) { if (typeof arg === ‘string’) { console.log(arg.toUpperCase()); // ^? } }

这个例子中,我们检查 `arg` 是否为 `string` 类型。TypeScript 识别出了 `typeof arg === "string"` 检查,它被当作是一个类型守卫,并且知道在 `if` 分支内 `arg` 的类型为 `string`。这样就可以正常地访问 `string` 类型上的方法,例如 `toUpperCase()`。但如果我们将条件表达式提取到一个名为 `argIsString` 的常量会发生什么?```ts// 在 TS 4.3 及以下版本function foo(arg: unknown) {    const argIsString = typeof arg === 'string';    if (argIsString) {        console.log(arg.toUpperCase());        //              ~~~~~~~~~~~        // 错误!'unknown' 类型上不存在 'toUpperCase' 属性。    }}

在之前版本的 TypeScript 中,这样做会产生错误 - 就算 argIsString 的值为类型守卫,TypeScript 也会丢掉这个信息。 这不是想要的结果,因为我们可能想要在不同的地方重用这个检查。 为了绕过这个问题,通常需要重复多次代码或使用类型断言。

在 TypeScript 4.4 中,情况有所改变。 上面的例子不再产生错误! 当 TypeScript 看到我们在检查一个常量时,会额外检查它是否包含类型守卫。 如果那个类型守卫操作的是 const 常量,某个 readonly 属性或某个未修改的参数,那么 TypeScript 能够对该值进行类型细化。

不同种类的类型守卫都支持,不只是 typeof 类型守卫。 例如,对于可辨识联合类型同样适用。

```ts twoslash type Shape = | { kind: ‘circle’; radius: number } | { kind: ‘square’; sideLength: number };

function area(shape: Shape): number { const isCircle = shape.kind === ‘circle’; if (isCircle) { // 知道此处为 circle return Math.PI shape.radius * 2; } else { // 知道此处为 square return shape.sideLength 2; } }

在 TypeScript 4.4 版本中对判别式的分析又进了一层 - 现在可以提取出判别式然后细化原来的对象类型。```ts twoslashtype Shape =    | { kind: 'circle'; radius: number }    | { kind: 'square'; sideLength: number };function area(shape: Shape): number {    // Extract out the 'kind' field first.    const { kind } = shape;    if (kind === 'circle') {        // We know we have a circle here!        return Math.PI * shape.radius ** 2;    } else {        // We know we're left with a square here!        return shape.sideLength ** 2;    }}

另一个例子,该函数会检查它的两个参数是否有内容。

```ts twoslash function doSomeChecks( inputA: string | undefined, inputB: string | undefined, shouldDoExtraWork: boolean ) { const mustDoWork = inputA && inputB && shouldDoExtraWork; if (mustDoWork) { // We can access ‘string’ properties on both ‘inputA’ and ‘inputB’! const upperA = inputA.toUpperCase(); const upperB = inputB.toUpperCase(); // … } }

TypeScript 知道如果 `mustDoWork` 为 `true` 那么 `inputA` 和 `inputB` 都存在。也就是说不需要编写像 `inputA!` 这样的非空断言的代码来告诉 TypeScript `inputA` 不为 `undefined`。一个好的性质是该分析同时具有可传递性。TypeScript 可以通过这些常量来理解在它们背后执行的检查。```ts twoslashfunction f(x: string | number | boolean) {    const isString = typeof x === 'string';    const isNumber = typeof x === 'number';    const isStringOrNumber = isString || isNumber;    if (isStringOrNumber) {        x;        //  ^?    } else {        x;        //  ^?    }}

注意这里会有一个截点 - TypeScript 并不是毫无限制地去追溯检查这些条件表达式,但对于大多数使用场景而言已经足够了。

这个功能能让很多直观的 JavaScript 代码在 TypeScript 里也好用,而不会妨碍我们。 更多详情请参考 PR!

Symbol 以及模版字符串索引签名

TypeScript 支持使用索引签名来为对象的每个属性定义类型。 这样我们就可以将对象当作字典类型来使用,把字符串放在方括号里来进行索引。

例如,可以编写由 string 类型的键映射到 boolean 值的类型。 如果我们给它赋予 boolean 类型以外的值会报错。

```ts twoslash interface BooleanDictionary {

[key: string]: boolean;

}

declare let myDict: BooleanDictionary;

// 允许赋予 boolean 类型的值 myDict[‘foo’] = true; myDict[‘bar’] = false;

// 错误 myDict[‘baz’] = ‘oops’;

虽说在这里 [`Map` 可能是更适合的数据结构](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)(具体的说是 `Map<​string, boolean>`),但 JavaScript 对象通常更方便或者正是我们要操作的目标。相似地,`Array<​T>` 已经定义了 `number` 索引签名,我们可以插入和获取 `T` 类型的值。```ts// 这是 TypeScript 内置的部分 Array 类型interface Array<​T> {    [index: number]: T;    // ...}let arr = new Array<​string>();// 没问题arr[0] = 'hello!';// 错误,期待一个 'string' 值arr[1] = 123;

索引签名是一种非常有用的表达方式。 然而,直到现在它们只能使用 string 和 number 类型的键(string 索引签名存在一个有意为之的怪异行为,它们可以接受 number 类型的键,因为 number 会被转换为字符串)。 这意味着 TypeScript 不允许使用 symbol 类型的键来索引对象。 TypeScript 也无法表示由一部分 string 类型的键组成的索引签名 - 例如,对象属性名是以 data- 字符串开头的索引签名。

TypeScript 4.4 解决了这个问题,允许 symbol 索引签名以及模版字符串。

例如,TypeScript 允许声明一个接受任意 symbol 值作为键的对象类型。

```ts twoslash interface Colors {

[sym: symbol]: number;

}

const red = Symbol(‘red’); const green = Symbol(‘green’); const blue = Symbol(‘blue’);

let colors: Colors = {};

// 没问题 colors[red] = 255; let redVal = colors[red]; // ^ number

colors[blue] = ‘da ba dee’; // 错误:’string’ 不能赋值给 ‘number’

相似地,可以定义带有模版字符串的索引签名。一个场景是用来免除对以 `data-` 开头的属性名执行的 TypeScript 额外属性检查。当传递一个对象字面量给目标类型时,TypeScript 会检查是否存在相比于目标类型的额外属性。```tsinterface Options {    width?: number;    height?: number;}let a: Options = {    width: 100,    height: 100,    'data-blah': true,};interface OptionsWithDataProps extends Options {    // 允许以 'data-' 开头的属性    [optName: `data-${string}`]: unknown;}let b: OptionsWithDataProps = {    width: 100,    height: 100,    'data-blah': true,    // 使用未知属性会报错,不包括以 'data-' 开始的属性    'unknown-property': true,};

最后,索引签名现在支持联合类型,只要它们是无限域原始类型的联合 - 尤其是:

  • string

  • number

  • symbol

  • 模版字符串(例如 hello-${string} )

带有以上类型的联合的索引签名会展开为不同的索引签名。

interface Data {    [optName: string | symbol]: any;}// 等同于interface Data {    [optName: string]: any;    [optName: symbol]: any;}

更多详情请参考 PR。

Defaulting to the unknown Type in Catch Variables (--useUnknownInCatchVariables)

异常捕获变量的类型默认为 unknown (--useUnknownInCatchVariables)

在 JavaScript 中,允许使用 throw 语句抛出任意类型的值,并在 catch 语句中捕获它。 因此,TypeScript 从前会将异常捕获变量的类型设置为 any 类型,并且不允许指定其它的类型注解:

try {    // 谁知道它会抛出什么东西    executeSomeThirdPartyCode();} catch (err) {    // err: any    console.error(err.message); // 可以,因为类型为 'any'    err.thisWillProbablyFail(); // 可以,因为类型为 'any' :(}

当 TypeScript 引入了 unknown 类型后,对于追求高度准确性和类型安全的用户来讲在 catch 语句的捕获变量处使用 unknown 成为了比 any 类型更好的选择,因为它强制我们去检测要使用的值。 后来,TypeScript 4.0 允许用户在 catch 语句中明确地指定 unknown(或 any)类型,这样就可以根据情况有选择一使用更严格的类型检查; 然而,在每一处 catch 语句里手动指定 : unknown 是一件繁琐的事情。

因此,TypeScript 4.4 引入了一个新的标记 --useUnknownInCatchVariables。 它将 catch 语句捕获变量的默认类型由 any 改为 unknown。

declare function executeSomeThirdPartyCode(): void;try {    executeSomeThirdPartyCode();} catch (err) {    // err: unknown    // Error! Property 'message' does not exist on type 'unknown'.    console.error(err.message);    // Works! We can narrow 'err' from 'unknown' to 'Error'.    if (err instanceof Error) {        console.error(err.message);    }}

这个标记属性于 --strict 标记家族的一员。 也就是说如果你启用了 --strict,那么该标记也自动启用了。 在 TypeScript 4.4 中,你可能会看到如下的错误:

Property 'message' does not exist on type 'unknown'.Property 'name' does not exist on type 'unknown'.Property 'stack' does not exist on type 'unknown'.

如果我们不想处理 catch 语句中 unknown 类型的捕获变量,那么可以明确使用 : any 类型注解,这样就会关闭严格类型检查。

```ts twoslash declare function executeSomeThirdPartyCode(): void;

try { executeSomeThirdPartyCode(); } catch (err: any) { console.error(err.message); // Works again! }

更多详情请参考 [PR](https://github.com/microsoft/TypeScript/pull/41013)。## 确切的可选属性类型 (`--exactOptionalPropertyTypes`)在 JavaScript 中,读取对象上某个不存在的属性会得到 `undefined` 值。与此同时,某个已有属性的值也允许为 `undefined` 值。有许多 JavaScript 代码都会对这些情况一视同仁,因此最初 TypeScript 将可选属性视为添加了 `undefined` 类型。例如,```tsinterface Person {    name: string;    age?: number;}

等同于:

interface Person {    name: string;    age?: number | undefined;}

这意味着用户可以给 age 明确地指定 undefined 值。

const p: Person = {    name: 'Daniel',    age: undefined, // This is okay by default.};

因此默认情况下,TypeScript 不区分带有 undefined 类型的属性和不存在的属性。 虽说这在大部分情况下是没问题的,但并非所有的 JavaScript 代码都如此。 像是 Object.assign,Object.keys,对象展开({ ...obj })和 for-in 循环这样的函数和运算符会区别对待属性是否存在于对象之上。 在 Person 例子中,如果 age 属性的存在与否是至关重要的,那么就可能会导致运行时错误。

在 TypeScript 4.4 中,新的 --exactOptionalPropertyTypes 标记指明了可选属性的确切表示方式,即不自动添加 | undefined 类型:

```ts twoslash interface Person { name: string; age?: number; }

// 启用 ‘exactOptionalPropertyTypes’ const p: Person = { name: ‘Daniel’, age: undefined, // 错误!undefined 不是一个成员 };