开始逐渐体操化
先来看一些内置的 TS 工具
1 2 3 4 5 6 7 8 9
| type Person = { name: string; age: number; }
type X1 = Readonly<Person> type X2 = Partial<Person> type X3 = Required<Person> type X4 = Record<string, number> type X5 = Exclude<1 | 2 | 3, 1 | 2> type X6 = Extract<1 | 2 | 3, 2 | 4> type X7 = Omit<Person, 'name' | 'age'>
|
Readonly
1 2 3
| type Readonly<T> = { readonly [K in keyof T]: T[K] }
|
关键词 in
,而不是用 :
,上之前的文章种也提到过,in 多用于泛型
Partial
1 2 3
| type Partial<T> = { [K in keyof T]?: T[K] }
|
Required
1 2 3
| type Required<T> = { [K in keyof T]-?: T[K] }
|
Record
1 2 3
| type Record<K extends string | number | symbol, V> = { [key in K]: V }
|
Exclude
1
| type Exclude<T, K> = T extends K ? never : T
|
1
| type Extract<T, K> = T extends K ? T: never
|
Omit
1 2 3
| type Omit<T, K> = { [K2 in keyof T as (K2 extends K ? never : K2)]: T[K2] }
|
Pick
1 2 3
| type Pick<T, K extends keyof T> = { [K2 in keyof K]: T[K2] }
|
这个时候在来实现 Omit 更加简单
1
| type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
|
Mutable
1 2 3
| type Mutable<T> = { -readonly [K in keyof T]: T[K] }
|
结论
理论上 TS 可以完成所有 JS 的功能
超牛的项目
象棋
https://github.com/chinese-chess-everywhere/type-chess
lisp 解释器
型体操天花板
https://zhuanlan.zhihu.com/p/427309936