一、Partial、Required、Readonly

转化为可选参数/必传参数/只读属性

  1. // 1,Partial转化为可选参数
  2. interface PartialData {
  3. title: string;
  4. description: string;
  5. }
  6. type PartialModel = Partial<PartialData>
  7. // 相当于
  8. // type PartialModel = {
  9. // title?: string | undefined;
  10. // description?: string | undefined;
  11. // }
  12. // 2,Required把可选转为必选
  13. interface Props {
  14. a?: number;
  15. b?: string;
  16. }
  17. const obj: Props = { a: 5 }; // OK
  18. const obj2: Required<Props> = { a: 5 }; // 错误:类型 "{ a: number; }" 中缺少属性 "b",但类型 "Required<Props>" 中需要该属性。
  19. // 3,Readonly转化为只读属性
  20. interface Todo {
  21. title: string;
  22. }
  23. const todo1: Readonly<Todo> = {
  24. title: 'Delete inactive users',
  25. };
  26. todo1.title = 'Hello'; // 错误:无法分配到 "title" ,因为它是只读属性。
  27. const todo2 = Object.freeze({ // Object.freeze可以把对象冻结为只读
  28. title: 'Delete inactive users',
  29. })
  30. todo2.title = 'Hello'; // 无法分配到 "title" ,因为它是只读属性。

二、Record

将某个类型的属性映射到另一个类型上

  1. interface PageInfo {
  2. title: string;
  3. }
  4. type Page = 'home' | 'about' | 'contact';
  5. const x: Record<Page, PageInfo> = { // Record<Page, PageInfo>相当于:type X = {[key in Page]:PageInfo}
  6. about: { title: 'about' },
  7. contact: { title: 'contact' },
  8. home: { title: 'home' },
  9. };

三、Pick、Omit

从接口<第一个参数>中挑选/剔除部分属性<第二个参数>来构造类型。

  1. interface Todo {
  2. title: string;
  3. description: string;
  4. completed: boolean;
  5. }
  6. type TodoPreviewPick = Pick<Todo, 'title' | 'completed'>; // Pick
  7. type TodoPreviewOmit = Omit<Todo, 'description'>; // Omit
  8. const todo1: TodoPreviewPick = {
  9. title: 'Clean room',
  10. completed: false,
  11. };
  12. const todo2: TodoPreviewOmit = {
  13. title: 'Clean room',
  14. completed: false,
  15. };

四、Exclude、Extract

第一个参数必须要是Type类型,并从里面剔除/提取 第二个参数的属性,然后构造一个类型。

  1. // Exclude
  2. type T0 = Exclude<'a' | 'b' | 'c', 'a'>; // "b" | "c"
  3. type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>; // "c"
  4. type T2 = Exclude<string | number | (() => void), Function>; // string | number
  5. // Extract
  6. type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>; // "a"
  7. type T1 = Extract<string | number | (() => void), Function>; // () => void

五、NonNullable

从类型 Type 中剔除 null 和 undefined ,然后构造一个类型。

  1. type T0 = NonNullable<string | number | undefined>; // string | number
  2. type T1 = NonNullable<string[] | null | undefined>; // string[]

六、Parameters

把函数的参数作为元组返回一个新的类型

  1. declare function f1(arg: { a: number; b: string }): void;
  2. type T0 = Parameters<() => string>;
  3. // []
  4. type T1 = Parameters<(s: string) => void>;
  5. // [s: string]
  6. type T2 = Parameters<<T>(arg: T) => T>;
  7. // [arg: unknown]
  8. type T3 = Parameters<typeof f1>;
  9. // [arg: { a: number; b: string; }]
  10. type T4 = Parameters<any>;
  11. // unknown[]
  12. type T5 = Parameters<never>;
  13. // never
  14. type T6 = Parameters<string>;
  15. // never
  16. // 类型“string”不满足约束“(...args: any) => any”。
  17. type T7 = Parameters<Function>;
  18. // never
  19. // 类型“string”不满足约束“(...args: any) => any”。

七、ConstructorParameters?不理解有啥用

由构造函数类型 Type 的参数类型来构建出一个元组类型。(若 Type 不是构造函数类型,则返回 never )。

  1. type T0 = ConstructorParameters<ErrorConstructor>;
  2. // [message?: string | undefined]
  3. type T1 = ConstructorParameters<FunctionConstructor>;
  4. // string[]
  5. type T2 = ConstructorParameters<RegExpConstructor>;
  6. // [pattern: string | RegExp, flags?: string | undefined]
  7. type T3 = ConstructorParameters<any>;
  8. // unknown[]
  9. type T4 = ConstructorParameters<Function>;
  10. // never
  11. // 类型“Function”不满足约束“abstract new (...args: any) => any”。
  12. // 类型“Function”提供的内容与签名“new (...args: any): any”不匹配

八、ReturnType

由函数类型的返回值 来构建一个新类型。

  1. declare function f1(arg: { a: number; b: string }): { a: number; b: string };
  2. type T0 = ReturnType<() => string>; // string
  3. type T1 = ReturnType<(s: string) => void>; // void
  4. type T2 = ReturnType<(<T>() => T)>; // unknown
  5. type T3 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]
  6. type T4 = ReturnType<typeof f1>; // { a: number, b: string }
  7. type T5 = ReturnType<any>; // any
  8. type T6 = ReturnType<never>; // never
  9. type T7 = ReturnType<string>; // 错误:类型“string”不满足约束“(...args: any) => any”。
  10. type T8 = ReturnType<Function>; // 错误:类型“Function”不满足约束“(...args: any) => any”。类型“Function”提供的内容与签名“(...args: any): any”不匹配。

九、InstanceType

由构造函数类型的实例类型 来构建一个新类型。

  1. type T0 = InstanceType<typeof C>; // C
  2. type T1 = InstanceType<any>; // any
  3. type T2 = InstanceType<never>; // never
  4. type T3 = InstanceType<string>; // 错误:类型“string”不满足约束“abstract new (...args: any) => any”。
  5. type T4 = InstanceType<Function>; // 错误:类型“Function”不满足约束“abstract new (...args: any) => any”。类型“Function”提供的内容与签名“new (...args: any): any”不匹配。

十、ThisParameterType

从函数类型中提取 this 参数的类型。 若函数类型不包含 this 参数,则返回 unknown 类型。

  1. function toHex(this: Number) {
  2. return this.toString(16);
  3. }
  4. function numberToString(n: ThisParameterType<typeof toHex>) {
  5. return toHex.apply(n);
  6. }
  7. const re = numberToString(1); // "1"