HomeToolsAbout

Pick and Omit

// original type type Bird = { name: string; hasWings: boolean; hasLegs: boolean; color: string[]; fly: () => void; }; // pick (select a few) type SubBird = Pick<Bird, "name" | "hasLegs"> // omit (unselect a few) type UnnamedBird = Omit<Bird, "name"> // is () function isBird(t: any): t is Bird { ... };

Pick Type

Picks the set of properties defined from the original type.

// example from doc interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Pick<Todo, "title" | "completed">; const todo: TodoPreview = { title: "Clean room", completed: false, };

Omit Type

Picks all properties from the original type, then removes the defined key from the type.

Opposite of Pick.

// syntax type OmittedType = Omit<TypeName, 'key_name'> // example from doc interface Todo { title: string; description: string; completed: boolean; createdAt: number; } type TodoPreview = Omit<Todo, "description">; const todo: TodoPreview = { title: "Clean room", completed: false, createdAt: 1615544252770, };
AboutContact