22. Explain the difference between Omit and Pick utility types in TypeScript. Provide examples.

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.

devFlipCards 2025

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.