Skip to content

Commit aa3d1b1

Browse files
committed
Merge 2022 summer doc TS complex types
1 parent cf63b7a commit aa3d1b1

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed

docs/languages/typescript/type.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
# TypeScript 的复杂类型
2+
3+
## 泛型
4+
5+
与其他一些支持泛型的语言不同,TypeScript 的泛型并不会为每一种类型生成一份代码,只是检查是否每一种可能的类型都可以兼容,在运行中仍然使用同一份代码。
6+
7+
可以在声明函数、类、接口和类型时声明泛型:
8+
9+
```typescript
10+
function identity<Type>(arg: Type): Type {
11+
return arg;
12+
}
13+
14+
const identity = <Type>(arg: Type): Type => arg;
15+
16+
class GenericNumber<NumType> {
17+
zeroValue: NumType;
18+
add: (x: NumType, y: NumType) => NumType;
19+
}
20+
21+
interface Request<ReqBody, ResBody> {
22+
request: ReqBody;
23+
response: ResBody;
24+
}
25+
26+
type MaybeArray<Value> = Value | Value[];
27+
```
28+
29+
可以通过以下方式使用前面定义的泛型:
30+
31+
```typescript
32+
identity<number>(1);
33+
34+
const num = new GenericNumber<number>();
35+
36+
const req: Request<{ action: string }, { result: string }> = {
37+
request: {
38+
action: 'update system',
39+
},
40+
response: {
41+
result: 'succeeded',
42+
},
43+
};
44+
45+
let nums: MaybeArray<number> = 0;
46+
nums = [0, 1, 2];
47+
```
48+
49+
可以为泛型添加限制和默认值,也可以由编译器推断泛型类型:
50+
51+
```typescript
52+
function identity<Type = number>(arg: Type): Type {
53+
return arg;
54+
}
55+
56+
identity(1);
57+
58+
function identity<Type extends { data: string }>(arg: Type): string {
59+
return arg.data;
60+
}
61+
62+
identity({ data: '' }); // inferred Type = { data: string }
63+
```
64+
65+
需要注意的是泛型只能使用类型作为参数,如果需要某个值的类型,可以使用 `typeof`
66+
67+
## `keyof``typeof` 关键字
68+
69+
`typeof` 除了可以作为运算符获取变量类型以外,用作类型标注时,可以获取变量的具体类型(而非作为运算符时的有限种类):
70+
71+
```typescript
72+
const someObj = {
73+
foo: 1,
74+
bar: '2',
75+
};
76+
77+
// inferred arg: { foo: number; bar: string }
78+
function f(arg: typeof someObj) {
79+
/* do something */
80+
}
81+
```
82+
83+
`keyof` 可以获取**类型**的键的类型(作为枚举):
84+
85+
```typescript
86+
const someObj = {
87+
foo: 1,
88+
bar: '2',
89+
};
90+
91+
// inferred arg: 'foo' | 'bar'
92+
function f(arg: keyof typeof someObj) {
93+
/* do something */
94+
}
95+
```
96+
97+
但 TypeScript 的一个缺陷是,使用 `for...in` 语法时并不会自动推断为 `keyof`,例如:
98+
99+
```typescript
100+
const someObj = {
101+
foo: 1,
102+
bar: '2',
103+
};
104+
105+
for (const key in someObj) {
106+
// inferred key: string, value: any
107+
const value = someObj[key];
108+
}
109+
```
110+
111+
## 访问类型内部
112+
113+
通过这种方式可以获取一个类型中的部分内容:
114+
115+
```typescript
116+
interface SomeComplexType {
117+
foo: {
118+
bar: string;
119+
}[];
120+
}
121+
122+
type Foo = SomeComplexType['foo']; // Foo = { bar: string }[]
123+
// 以下两种等价
124+
type Bar = Foo[number]; // Bar = { bar: string }
125+
type Bar = Foo[0]; // Bar = { bar: string }
126+
```
127+
128+
## 条件类型
129+
130+
可以使用类似三目运算符的语法来编写条件类型:
131+
132+
```typescript
133+
interface Animal {
134+
live(): void;
135+
}
136+
137+
interface Dog extends Animal {
138+
woof(): void;
139+
}
140+
141+
type Example1 = Dog extends Animal ? number : string; // Example1 = number
142+
143+
type Example2 = RegExp extends Animal ? number : string; // Example2 = string
144+
```
145+
146+
这种语法与泛型结合尤为有用:
147+
148+
```typescript
149+
type MessageOf<T> = T extends { message: any } ? T['message'] : unknown;
150+
151+
interface Email {
152+
message: string;
153+
}
154+
155+
interface Dog {
156+
bark(): void;
157+
}
158+
159+
type EmailMessageContents = MessageOf<Email>; // EmailMessageContents = string
160+
161+
type DogMessageContents = MessageOf<Dog>; // DogMessageContents = unknown
162+
```
163+
164+
## 只读、可选
165+
166+
通过以下方式标记某一属性为只读或可选:
167+
168+
```typescript
169+
interface SomeType {
170+
readonly readonlyProperty: string;
171+
optionalProperty?: string;
172+
}
173+
```
174+
175+
需要注意的是,这些修饰只会在编译时检查是否违反,在编译后运行时你仍然可能打破这些限制,在使用 JavaScript 调用 TypeScript 库时尤其可能存在。
176+
177+
## 实用内置类型
178+
179+
TypeScript 内置了多种实用类型,可以帮助我们更简洁地标注一些复杂类型,以下仅做部分展示:
180+
181+
```typescript
182+
interface Data {
183+
key1?: string;
184+
key2: string;
185+
}
186+
187+
// 以下相同名称的类型内容相同
188+
189+
// 全部属性变为可选
190+
type PartialData = Partial<Data>;
191+
type PartialData = {
192+
key1?: string;
193+
key2?: string;
194+
};
195+
196+
// 全部属性变为必选
197+
type FullData = Required<PartialData>;
198+
type FullData = {
199+
key1: string;
200+
key2: string;
201+
};
202+
203+
// 全部属性变为只读
204+
type ReadonlyData = Readonly<Data>;
205+
type ReadonlyData = {
206+
readonly key1?: string;
207+
readonly key2: string;
208+
};
209+
210+
// 特定类型之间的映射
211+
type DataDict = Record<string, Data>;
212+
type DataDict = {
213+
// 这种语法表示任意 string 的值 key 都可以作为键
214+
[key: string]: Data;
215+
};
216+
217+
// 选择部分属性
218+
type DataKey1 = Pick<Data, 'key1'>;
219+
// 忽略部分属性
220+
type DataKey1 = Omit<Data, 'key2'>;
221+
type DataKey1 = {
222+
key1?: string;
223+
};
224+
225+
function f(a: number, b: string, c: number[]): number {
226+
return 0;
227+
}
228+
// 获取参数列表
229+
type Args = Parameters<typeof f>;
230+
type Args = [a: number, b: string, c: number[]];
231+
232+
type Ret = ReturnType<typeof f>;
233+
type Ret = number;
234+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ nav:
125125
- TypeScript 的变量与变量类型: languages/typescript/variable.md
126126
- TypeScript 的函数: languages/typescript/function.md
127127
- TypeScript 的面向对象: languages/typescript/oop.md
128+
- TypeScript 的复杂类型: languages/typescript/type.md
128129
- Rust 语言基础:
129130
- languages/rust/index.md
130131
- Rust 入门的一点经验: languages/rust/whyrust.md

0 commit comments

Comments
 (0)