Typescript 内置类型

Typescript 内置类型

Partial

1
2
3
4
5
// typescript/lib/lib.es5.d.ts

type Partial<T> = {
[P in keyof T]?: T[P];
};

Partial 类型的作用将对应类型的属性调整为可选属性。

1
2
3
4
5
6
interface PERSON {
name: string;
age: number;
}

type OPTIONAL_PERSON = Partial<PERSON>;

Partial 类型无法将嵌套属性变为可选的属性,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface ADDRESS {
province: string;
city: string;
county: string;
}

interface PERSON {
name: string;
age: number;
address: ADDRESS
}

type OPTIONAL_PERSON = Partial<PERSON>;

// Type '{ province: string; }' is missing the following properties from type 'ADDRESS': city, county(2739)
let xiaoming: OPTIONAL_PERSON = {
name: '小明',
age: 18,
address: {
province: '上海'
}
}

如果需要将嵌套属性调整为可选属性,可采用递归的思路将 Partial 进行改造:

1
2
3
4
// 需要 TS 2.8+
type NestPartial<T> = {
[K in keyof T]?: T[K] extends object ? NestPartial<T[K]> : T[K]
}

Conditional

TS 2.8 中引入了 Conditional Types,该类型通过表达式进行类型关系检测,用法与三目表达式类似。

1
T extends U ? X : Y
1
2
3
4
5
6
7
8
9
10
11
12
13
type TypeName<T> =
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends Function ? "function" :
"object";

type T0 = TypeName<string>; // "string"
type T1 = TypeName<"a">; // "string"
type T2 = TypeName<true>; // "boolean"
type T3 = TypeName<() => void>; // "function"
type T4 = TypeName<string[]>; // "object"

如果 T 是联合类型,那么在实例化时会自动分发成联合类型。

1
2
// T 为 A | B | C
T extends U ? X : Y => (A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y)

Required

1
2
3
type Required<T> = {
[P in keyof T]-?: T[P];
};

与 Partial 相反,Required 类型是将对应类型的属性调整为必须属性。-? 表示移除可选修饰符。

1
2
3
4
5
6
7
8
9
10
11
12
13
interface ADDRESS {
province: string;
city: string;
county: string;
}

interface PERSON {
name: string;
age: number;
address?: ADDRESS
}

type REQUIRED_PERSON = Required<PERSON>;

![image-20200109114351206](/Users/zhanghexuan/Library/Application Support/typora-user-images/image-20200109114351206.png)

Readonly

1
2
3
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};

将对应类型的所有属性调整为只读。

1
2
3
4
5
6
7
8
9
10
11
12
13
interface ADDRESS {
province: string;
city: string;
county: string;
}

interface PERSON {
name: string;
age: number;
address?: ADDRESS
}

type READONLY_PERSON = Readonly<PERSON>;

Pick

1
2
3
type pick<T, K extends keyof T> {
[P in K]: T[P];
}

从对应类型中选取某个属性

1
2
3
4
5
6
7
8
9
10
11
12
13
interface ADDRESS {
province: string;
city: string;
county: string;
}

interface PERSON {
name: string;
age: number;
address?: ADDRESS
}

type PICK_PERSON = Pick<PERSON, 'name' | 'age'>;

Record

1
2
3
type Record<K extends keyof any, T> = {
[P in K]: T;
};

将 K 中所有的属性类型设置为 T 类型

1
type PERSON_LIST = Record<'list' | 'data', PERSON[]>;

Exclude

1
type Exclude<T, U> = T extends U ? never : T;

将类型 T 中的某些属于 U 的类型移除掉

1
type Demo = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "b" | "d"

继承并重定义部分属性类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface ADDRESS {
province: string;
city: string;
county: string;
}

interface PERSON {
name: string;
age: number;
address?: ADDRESS
}

type Filter<T, U> = Pick<T, Exclude<keyof T, U>>;

interface Demo extends Filter < PERSON, 'address' > {
address: string;
}

Extract

1
type Extract<T, U> = T extends U ? T : never;

提取类型 T 和类型 U 共同的属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface ADDRESS {
province: string;
city: string;
county: string;
}

interface PERSON {
name: string;
age: number;
address?: ADDRESS;
gender: string | undefined
}

interface ANIMAL {
name: string;
age: number;
}

![image-20200109145355244](/Users/zhanghexuan/Library/Application Support/typora-user-images/image-20200109145355244.png)

ReturnType

1
type ReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : any;

通过 Conditional Types 推断(infer)T 的返回类型,从而获取方法的返回类型。

1
2
3
const log = (msg: string) => console.log(msg);

type RETURN_TYPE = ReturnType<typeof log>;

ThisType

1
interface ThisType<T> { }

该类型用于指定上下文对象类型。

1
2
3
4
5
const Male = {
log(this: PERSON) {
console.log(this.name);
}
}

NonNullable

1
type NonNullable<T> = T extends null | undefined ? never : T;

该类型用于过滤类型 T 中的 null 及 undefined 类型。