TS内置的高级和工具类型

643 词

TS内置的高级和工具类型

Parameters 获取函数参数类型

获取函数参数类型

1
2
3
4
5
6
7
const func = (name: string):number => {
return 0;
}

type funcParam = Parameters<typeof func>;

// type funcParam = [name: string]

底层原理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
```

## ReturnType 获取函数返回值类型

> 获取函数返回值类型

``` typescript
const func = (name: string):number => {
return 0;
}

type funcParam = ReturnType<typeof func>;

// type funcParam = number

底层原理:

1

ConstructorParameters 获取构造器的参数类型

InstanceType 提取构造器返回值的类型

ThisParameterType 提取this的参数类型

Partial 把所有索引变为可选

Required 把所有索引去掉可选

Readonly 把所有索引改成只读

Pick 提取索引的类型

Record 创建索引类型

Exclude 从一个联合类型中去掉一部分类型

Extract 提取联合类型的交叉类型

Omit 剔除索引类型的部分索引

Awaited

NonNullable

Uppercase、Lowercase、Capitalize、Uncapitalize

留言