Skip to content

Commit ddb4ae9

Browse files
committed
feat: infer
1 parent 96aecd3 commit ddb4ae9

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1.0.0 (2020-10-04)
1+
# 1.0.0 (2020-10-10)
22

33

44
### Bug Fixes

docs/.vuepress/styles/index.styl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
#app {
22
.page {
3+
padding-left: 0;
34
#gitalk-container {
45
margin: auto;
56
width: 60%;
67
}
8+
.theme-default-content:not(.custom) {
9+
max-width: 840px;
10+
}
711
}
812

13+
.page-nav, .page-edit {
14+
max-width: 840px;
15+
}
16+
917
details {
1018
border-radius: 4px;
1119
padding: .5em .5em;

docs/zh/keyword/infer/README.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,106 @@
11
## 定义
22

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`, 编译器确保您已经显式声明了所有类型变量。
48

59
## 使用
610

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+
7104
## 快来耍耍啊
8105

9106
### 🌰🌰
@@ -33,3 +130,7 @@
33130
[infer PR: Type inference in conditional types](https://github.com/Microsoft/TypeScript/pull/21496)
34131

35132
[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

Comments
 (0)