ts内置工具类型
Record
Record<Keys, Type>定义一个对象的 key 和 value 类型
源码
1
2
3type Record<K extends keyof any, T> = {
[P in K]: T;
};使用
1
2
3
4
5
6type RecordType = Record<string, string|number>;
const recordExample: RecordType ={
a: 1,
b: "1"
}
Partial
Partial<Type>构造一个类型使Type的所有属性都设置为可选。
源码
1
2
3type Partial<T> = {
[P in keyof T]?: T[P];
};使用
1
2
3
4
5
6
7
8
9
10
11
12
13interface Example {
a: string;
b: number;
}
type PartialExample = Partial<Example>;
/**
* PartialExample
* interface {
* a?: string | undefined;
* b?: number | undefined;
* }
*/
Required
Required<Type>构造一个类型使Type的所有属性都设置为required,与Partial<Type>功能相反。
源码
1
2
3type Required<T> = {
[P in keyof T]-?: T[P];
};使用
1
2
3
4
5
6
7
8
9
10
11
12
13interface Example {
a?: string;
b?: number;
}
type RequiredExample = Required<Example>;
/**
* RequiredExample
* interface {
* a: string;
* b: number;
* }
*/
Readonly
Required<Type>构造一个类型使Type的所有属性都设置为readonly。
源码
1
2
3type Readonly<T> = {
readonly [P in keyof T]: T[P];
};使用
1
2
3
4
5
6
7
8
9
10
11
12
13interface Example {
a: string;
b: number;
}
type ReadonlyExample = Readonly<Example>;
/**
* ReadonlyExample
* interface {
* readonly a: string;
* readonly b: number;
* }
*/
Pick
Pick<Type, Keys>通过从Type中选择一组属性Keys来构造一个类型。
源码
1
2
3type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14interface Example {
a: string;
b: number;
c: symbol;
}
type PickExample = Pick<Example, "a"|"b">;
/**
* PickExample
* interface {
* a: string;
* b: number;
* }
*/
Omit
Omit<Type, Keys>通过从Type中选择所有属性然后删除Keys来构造一个类型,与Pick<Type, Keys>功能相反。
源码
1
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
使用
1
2
3
4
5
6
7
8
9
10
11
12
13interface Example {
a: string;
b: number;
c: symbol;
}
type OmitExample = Omit<Example, "a"|"b">;
/**
* OmitExample
* interface {
* c: symbol;
* }
*/
Exclude
Exclude<UnionType, ExcludedMembers>通过从UnionType中排除可分配给ExcludedMembers的所有联合成员来构造类型。
源码
1
type Exclude<T, U> = T extends U ? never : T;
使用
1
2
3
4
5
6type ExcludeExample = Exclude<"a"|"b"|"c"|"z", "a"|"b"|"d">;
/**
* ExcludeExample
* "c" | "z"
*/
Extract
Extract<Type, Union>通过从Type中提取所有可分配给Union的联合成员来构造一个类型,与Exclude<UnionType, ExcludedMembers>功能相反。
源码
1
type Extract<T, U> = T extends U ? T : never;
使用
1
2
3
4
5type ExtractExample = Extract<"a"|"b"|"c"|"z", "a"|"b"|"d">;
/**
* ExtractExample
* "a" | "b"
*/
NonNullable
NonNullable<Type>通过从Type中排除null和undefined来构造一个类型。
源码
1
type NonNullable<T> = T extends null | undefined ? never : T;
使用
1
2
3
4
5type NonNullableExample = NonNullable<number|string|null|undefined>;
/**
* NonNullableExample
* string | number
*/
Parameters
Parameters<Type>从函数类型Type的参数中使用的类型构造元组类型。
源码
1
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
使用
1
2
3
4
5
6
7type FnType = (a1: number, a2: string) => void;
type ParametersExample = Parameters<FnType>;
/**
* ParametersExample
* [a1: number, a2: string]
*/
infer
infer和extends配合,用作类型推断。
infer只能在条件类型的 extends 子句中使用infer得到的类型只能在true语句中使用
使用案例:
判断数组类型
1
type InferArray<T> = T extends (infer U)[] ? U : never;
判断函数参数类型(
Parameters)判断函数返回值类型(
ReturnType)判断元组最后一个元素的类型
1
type InferLast<T extends unknown[]> = T extends [... infer _, infer Last] ? Last : never;
判断字符串第一个字符的字面量类型
1
type InferString<T extends string> = T extends `${infer First}${infer _}` ? First : never;
ReturnType
ReturnType<Type>构造一个由函数Type的返回类型组成的类型。
源码
1
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
使用
1
2
3
4
5
6
7type FnType = (a1: number, a2: string) => string | number;
type ReturnTypeExample = ReturnType<FnType>;
/**
* ReturnTypeExample
* string | number
*/
ConstructorParameters
InstanceType
- 本文标题:ts内置工具类型
- 本文作者:灵感胜于汗水
- 创建时间:2022-11-01 14:10:03
- 本文链接:https://cjhsyc.github.io/2022/11/01/ts内置工具类型/
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!