Pick, 将T类型中的K属性挑出来

    1. interface Todo {
    2. title: string;
    3. description: string;
    4. completed: boolean;
    5. }
    6. type TodoPreview = Pick<Todo, 'title' | 'completed'>;
    7. const todo: TodoPreview = {
    8. title: 'Clean room',
    9. completed: false,
    10. };

    Omit, 将T类型中的K属性排除

    1. interface Todo {
    2. title: string;
    3. description: string;
    4. completed: boolean;
    5. }
    6. type TodoPreview = Omit<Todo, 'description'>;
    7. const todo: TodoPreview = {
    8. title: 'Clean room',
    9. completed: false,
    10. };

    Partial, 将T类型中的属性都变成可选属性。

    1. interface Todo {
    2. title: string;
    3. description: string;
    4. }
    5. function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
    6. return { ...todo, ...fieldsToUpdate };
    7. }
    8. const todo1 = {
    9. title: 'organize desk',
    10. description: 'clear clutter',
    11. };
    12. const todo2 = updateTodo(todo1, {
    13. description: 'throw out trash',
    14. });

    Required, 将T类型中的属性都变成必选属性

    1. interface Props {
    2. a?: number;
    3. b?: string;
    4. };
    5. const obj: Props = { a: 5 }; // OK
    6. const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing

    Extract, 从类型T中提取所有可以赋值给U的类型,然后构造一个类型

    1. type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a"
    2. type T1 = Extract<string | number | (() => void), Function>; // () => void

    Exclude,从类型T中剔除所有可以赋值给U的属性,然后构造一个类型

    1. type T0 = Exclude<"a" | "b" | "c", "a">; // "b" | "c"
    2. type T1 = Exclude<"a" | "b" | "c", "a" | "b">; // "c"
    3. type T2 = Exclude<string | number | (() => void), Function>; // string | number

    + -这两个关键字用于映射类型中给属性添加修饰符,比如-?就代表将可选属性变为必选,-readonly代表将只读属性变为非只读.

    1. type Required<T> = { [P in keyof T]-?: T[P] };
    1. /**
    2. * Make all properties in T optional
    3. */
    4. // 将 T 类型中的所有属性变成可选的
    5. type Partial<T> = {
    6. [P in keyof T]?: T[P];
    7. };
    8. /**
    9. * Make all properties in T required
    10. */
    11. // 将 T 类型中的所有属性变成必选的
    12. type Required<T> = {
    13. [P in keyof T]-?: T[P];
    14. };
    15. /**
    16. * Make all properties in T readonly
    17. */
    18. // 将 T 中所有属性变成只读的
    19. type Readonly<T> = {
    20. readonly [P in keyof T]: T[P];
    21. };
    22. /**
    23. * From T, pick a set of properties whose keys are in the union K
    24. */
    25. // 将 T 类型中的 K 属性挑出来
    26. type Pick<T, K extends keyof T> = {
    27. [P in K]: T[P];
    28. };
    29. /**
    30. * Construct a type with a set of properties K of type T
    31. */
    32. type Record<K extends keyof any, T> = {
    33. [P in K]: T;
    34. };
    35. /**
    36. * Exclude from T those types that are assignable to U
    37. */
    38. // 从类型 T 中去除所有可以赋值给 U 的属性,然后构造一个类型
    39. type Exclude<T, U> = T extends U ? never : T;
    40. /**
    41. * Extract from T those types that are assignable to U
    42. */
    43. // 取 T 类型 和 U 类型 属性的交集
    44. type Extract<T, U> = T extends U ? T : never;
    45. /**
    46. * Construct a type with the properties of T except for those in type K.
    47. */
    48. // 从 T 类型 中排除 K 属性
    49. type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

    示例:

    1. interface T {
    2. a: string;
    3. b: number;
    4. }
    5. interface U {
    6. a: string;
    7. }
    8. type A = Exclude<keyof T, keyof U>;
    9. const a: A = 'b'
    10. type B = Extract<keyof T, keyof U>;
    11. const b: B = 'a'
    12. type C = Omit<T, keyof U>;
    13. const c: C = {
    14. b: 1
    15. }