|
1 | 1 | ## 定义 |
2 | 2 |
|
3 | | -`infer` 最早出现在此 [PR](https://github.com/Microsoft/TypeScript/pull/21496) 中,表示在 `extends` 条件语句中待推断的类型变量。 |
| 3 | +`infer` 最早出现在此 [PR](https://github.com/Microsoft/TypeScript/pull/21496) 中, 表示在 `extends` 条件语句中待推断的类型变量。 |
| 4 | + |
| 5 | +`infer` 关键词常在条件类型中和 `extends` 关键词一同出现, 表示**将要推断的类型, 作为类型变量可以在三元表达式的 `true` 部分引用**。而 `ReturnType` 正是使用这种方式提取到了函数的返回类型。 |
| 6 | + |
| 7 | +使用 `infer`, 编译器确保您已经显式声明了所有类型变量。 |
4 | 8 |
|
5 | 9 | ## 使用 |
6 | 10 |
|
| 11 | +```ts |
| 12 | +type Unpacked<T> = T extends (infer U)[] |
| 13 | + ? U |
| 14 | + : T extends (...args: any[]) => infer U |
| 15 | + ? U |
| 16 | + : T extends Promise<infer U> |
| 17 | + ? U |
| 18 | + : T; |
| 19 | + |
| 20 | +type T0 = Unpacked<string>; // string |
| 21 | +type T1 = Unpacked<string[]>; // string |
| 22 | +type T2 = Unpacked<() => string>; // string |
| 23 | +type T3 = Unpacked<Promise<string>>; // string |
| 24 | +type T4 = Unpacked<Promise<string>[]>; // Promise<string> |
| 25 | +type T5 = Unpacked<Unpacked<Promise<string>[]>>; // string |
| 26 | +``` |
| 27 | + |
| 28 | +`infer` 可以用来推断联合类型 |
| 29 | + |
| 30 | +```ts |
| 31 | +type Foo<T> = T extends {a: infer U; b: infer U} ? U : never; |
| 32 | + |
| 33 | +type T10 = Foo<{a: string; b: string}>; // string |
| 34 | +type T11 = Foo<{a: string; b: number}>; // string | number |
| 35 | +``` |
| 36 | + |
| 37 | +同样,在变数位置中针对同一类型变量的多个候选会导致得出交集类型: |
| 38 | + |
| 39 | +```ts |
| 40 | +type Bar<T> = T extends {a: (x: infer U) => void; b: (x: infer U) => void} ? U : never; |
| 41 | +type T20 = Bar<{a: (x: string) => void; b: (x: string) => void}>; // string |
| 42 | +type T21 = Bar<{a: (x: string) => void; b: (x: number) => void}>; // string & number |
| 43 | +``` |
| 44 | + |
| 45 | +## 内置类型 |
| 46 | + |
| 47 | +### 用于提取函数类型的返回值类型 |
| 48 | + |
| 49 | +```ts |
| 50 | +/** |
| 51 | + * 获取函数 T 的返回类型 |
| 52 | + */ |
| 53 | +type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any; |
| 54 | +``` |
| 55 | + |
| 56 | +更多有关于 `ReturnType` [Here](../../utility-types/#returntype-t) |
| 57 | + |
| 58 | +#### Demo |
| 59 | + |
| 60 | +```ts |
| 61 | +type Func = (...args: any) => string | number; |
| 62 | + |
| 63 | +type ReturnValue = ReturnType<Func>; // string | number |
| 64 | +``` |
| 65 | + |
| 66 | +### 用于提取构造函数中参数 (实例) 类型 |
| 67 | + |
| 68 | +构造函数可以使用 `new` 来实例化, 因此它的类型通常表示如下: |
| 69 | + |
| 70 | +```ts |
| 71 | +type Constructor = new (...ages: any[]) => any; |
| 72 | +``` |
| 73 | + |
| 74 | +当 `infer` 用于构造函数类型中, 可用于参数位置 `new (...args: infer P) => any` 和 返回值位置 `new (...args: any[]) => infer P`。 |
| 75 | + |
| 76 | +因此就内置如下两个映射类型: |
| 77 | + |
| 78 | +```ts |
| 79 | +// 获取参数类型 |
| 80 | +type ConstructorParameters<T extends new (...args: any[]) => any> = T extends new ( |
| 81 | + ...args: infer P |
| 82 | +) => any |
| 83 | + ? P |
| 84 | + : never; |
| 85 | + |
| 86 | +// 获取实例类型 |
| 87 | +type InstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R |
| 88 | + ? R |
| 89 | + : any; |
| 90 | +``` |
| 91 | + |
| 92 | +#### Demo |
| 93 | + |
| 94 | +```ts |
| 95 | +class TestClass { |
| 96 | + constructor(public name: string, public string: number) {} |
| 97 | +} |
| 98 | + |
| 99 | +type Params = ConstructorParameters<typeof TestClass>; // [string, numbder] |
| 100 | + |
| 101 | +type Instance = InstanceType<typeof TestClass>; // TestClass |
| 102 | +``` |
| 103 | + |
7 | 104 | ## 快来耍耍啊 |
8 | 105 |
|
9 | 106 | ### 🌰🌰 |
|
33 | 130 | [infer PR: Type inference in conditional types](https://github.com/Microsoft/TypeScript/pull/21496) |
34 | 131 |
|
35 | 132 | [typescript-2-8](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html) |
| 133 | + |
| 134 | +[type-inference-in-conditional-types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#type-inference-in-conditional-types) |
| 135 | + |
| 136 | +[TypeScript 面试题](https://github.com/LeetCode-OpenSource/hire/blob/master/typescript_zh.md) |
0 commit comments