Middle
What are Pick and Omit in TypeScript?
sobes.tech AI
Answer from AI
In TypeScript, Pick and Omit are utility types that allow you to create new types based on existing ones by selecting or excluding certain properties.
Pick<T, K>creates a type consisting only of the propertiesKof objectT.Omit<T, K>creates a type excluding the propertiesKfrom objectT.
Example:
interface User {
id: number;
name: string;
email: string;
age: number;
}
// Select only id and name
type UserPreview = Pick<User, 'id' | 'name'>;
// Exclude email
type UserWithoutEmail = Omit<User, 'email'>;
This is useful for creating types with a specific set of fields without duplicating the description.