The Omit
and Pick
types are utility types in TypeScript that allow manipulating the properties of objects. Pick
allows selecting specific properties from an object, while Omit
allows removing specific properties from an object.
Example of Pick
:
interface Person { name: string; age: number; jobTitle: string; } type NameAndAge = Pick<Person, 'name' | 'age'>; // 'NameAndAge' only contains 'name' and 'age'
Example of Omit
:
interface Person { name: string; age: number; jobTitle: string; } type WithoutJobTitle = Omit<Person, 'jobTitle'>; // 'WithoutJobTitle' contains all properties except 'jobTitle'
Pick
is useful when you want to create a new type based on some properties of an existing object, while Omit
is useful when you want to remove one or more properties from an object.