TS:Omit、Pick、Exclude 定義新類型interface

Omit剔除某些字段

type User = {

id: string;

name: string;

email: string;

};

type UserWithoutEmail = Omit;
// UserWithoutEmail ={id: string;name: string;}

Pick選擇某些字段

      interface Todo {
        title: string;
        description: string;
        completed: boolean;
      }
      
      type TodoPreview = Pick;
      
      const todo: TodoPreview = {
        title: "Clean room",
        completed: false
      };

Exclude交集的反面

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

你可能感兴趣的:(前端,前端,typescript)