# Typescript 类型

# typeof

返回其类型

// 例如:
const a = { label: "bed", count: 1 };

type aType = typeof a;
// 可推断成如下
type aType = { label: string; count: number };
1
2
3
4
5
6

# Partial<T>

将类型 T 的所有属性标记为可选属性

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

# Required<T>

将类型 T 的所有属性标记为必填属性

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

# Readonly<T>

将类型 T 的所有属性标记为只读属性

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

# Pick<T, K>

将类型 T 的 K 属性保留,其他属性移除

type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
};

// 例如:
interface ContactPerson {
  name: string;
  age: number;
  phone: string;
  email: string;
}

type Person = Pick<ContactPerson, "phone" | "email">;
// 相等
type Person = {
  name: string;
  age: number;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Record<K, T>

构造一个类型为 T 的属性 K 的类型

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

// 例如:
const user: Record<"name" | "password", string> = {
  name: "Simon",
  password: "xxxxxxx",
};

const mapPerson: Record<number, Person> = {
  10001: {
    name: "Simon",
    age: "18",
    phone: "135xxxxxxxx",
    email: "naclyang@foxmail.com",
  },
  // ...
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Exclude<T, U>

移除 T 的 U

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

// 例如:
type Example = Exclude<"a" | "b" | "c" | "d", "a" | "b">;
// 相等
type Example = "c" | "d";
1
2
3
4
5
6

# Extract<T, U>

T 和 U 的交集

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

// 例如:
type Example = Extract<"a" | "b" | "c" | "d", "a" | "b">;
// 相等
type Example = "a" | "b";
1
2
3
4
5
6

# NonNullable<T>

排除 T 类型中的 null 和 undefined 类型

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

// 例如:
type Example = string | number | null;
type NonNullExample = NonNullable<Example>;
// 相等
type NonNullExample = string | number;
1
2
3
4
5
6
7

# Omit<T, K>

将类型 T 的 K 属性移除,其他属性滤出 和 Pick<T, K>很像,但一个是保留,这个是移除

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
1

# Parameters<T>

获取函数类型 T 的参数类型

type Parameters<T extends (...args: any) => any> = T extends (
  ...args: infer P
) => any
  ? P
  : never;

// 例如:
function add(a: number | string, b: number | string) {
  // ...
}
type addParams = Parameters<typeof add>;
// 相等
type addParams = [string | number, string | number];
1
2
3
4
5
6
7
8
9
10
11
12
13

# ConstructorParameters<T>

获取类构造函数类型 T 中的参数类型

type ConstructorParameters<
  T extends new (...args: any) => any
> = T extends new (...args: infer P) => any ? P : never;

// 例如:
class Person {
  name: string;
  age?: number;

  constructor(name: string, age?: number) {
    this.name = name;
    this.age = age;
  }
}

type PersonConstuctorParams = ConstructorParameters<typeof Person>;
// 相等
type PersonConstuctorParams = [string, (number | undefined)?];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# ReturnType<T>

获取函数类型 T 的返回类型

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

// 例如:
function add(a: number | string, b: number | string): number {
  // ...
}
type addReturn = ReturnType<typeof add>;
// 相等
type addReturn = number;
1
2
3
4
5
6
7
8
9
10
11
12
13

# InstanceType<T>

获取类的返回值类型

type InstanceType<T extends new (...args: any) => any> = T extends new (
  ...args: any
) => infer R
  ? R
  : any;
1
2
3
4
5