interface

partial

  1. interface a {
  2. name:string;
  3. age:number
  4. }
  5. type b = Partial<a>
  6. b
  7. type b = {
  8. name?: string | undefined;
  9. age?: number | undefined;
  10. }

required
readonly

pick

  1. interface a {
  2. name?:string;
  3. age?:number;
  4. id:number;
  5. }
  6. type c = Pick<a,'name'>
  7. //
  8. type c = {
  9. name?: string | undefined;
  10. }

omit

  1. interface a {
  2. name?:string;
  3. age?:number;
  4. id:number;
  5. }
  6. type c = Omit<a,'name'>
  7. //
  8. type c = {
  9. age?: number | undefined;
  10. id: number;
  11. }

record

定义对象的键值对类型

  1. interface PageInfo {
  2. title: string;
  3. }
  4. type Page = "home" | "about" | "contact";
  5. const nav: Record<Page, PageInfo> = {
  6. about: { title: "about" },
  7. contact: { title: "contact" },
  8. home: { title: "home" },
  9. };

type

Exclude — 从T中剔除可以赋值给U的类型。

  1. type a = Number | String | Boolean
  2. type b = Exclude<a,Number>
  3. b:
  4. type b = String | Boolean

Extract — 提取T中可以赋值给U的类型。
NonNullable — 从T中剔除null和undefined。
ReturnType — 获取函数返回值类型。
InstanceType — 获取构造函数类型的实例类型。