Sobes.tech
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 properties K of object T.
  • Omit<T, K> creates a type excluding the properties K from object T.

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.

What are Pick and Omit in TypeScript? — Frontend - sobes.tech