ProgrammingFrontend Developer

How does Pick work in TypeScript, what is this utility type for, and what is its difference from Omit?

Pass interviews with Hintsage AI assistant

Answer.

The utility type Pick<T, K> was introduced in TypeScript to address the problem of selectively copying part of the fields from an existing type or interface.

Background

Often during development, the same data structures are reused with slight variations. Before the introduction of Pick, developers had to create new interfaces, manually copying the necessary fields, which led to code duplication and errors during subsequent modifications of the original structures.

Problem

It is necessary to type the input or output data of a function using only part of the fields of a large interface to avoid redundancy and to specify the contract as precisely as possible.

Solution

Pick solves the problem of selecting a subset of properties of type T by keys K:

type Pick<T, K extends keyof T> = { [P in K]: T[P] };

Example:

interface User { id: number; name: string; email: string; } type Credentials = Pick<User, 'email' | 'name'>; const creds: Credentials = { email: "user@example.com", name: "User Name", };

Key features:

  • Forms a new type with only the necessary keys;
  • Very convenient for typing select queries, forms, outputs;
  • Minimizes manual code and errors during refactoring.

Trick questions.

How does Pick differ from Omit?

Pick selects only the listed fields of the type; Omit, conversely, excludes them from the structure.

type PartialUser = Omit<User, 'id'>; // All fields except id

Can Pick be used to select a non-existent property?

No, the keys K must necessarily be keys of the original type; otherwise, a compilation error will occur.

Can Pick affect the optionality or readonly properties?

Pick retains the property modifiers (optional, readonly) if they were in the original type, merely copying them to the new type.

Typing errors and anti-patterns

  • Explicit listing in Pick as a string ("id, name"), not as an array of keys, leading to compilation errors.
  • Selecting non-existent fields or incorrect auto-generation of keys.
  • Using Pick where exclusion or modification of properties is required — for this, Omit, Partial, Required exist.

Real-life example

Negative case

When working with a data entry form, the developer manually described their own type and forgot about a new mandatory attribute added to User. The form broke after the interface was updated.

Pros:

  • Explicit description of the structure.

Cons:

  • Risk of typedef obsolescence, high likelihood of bugs during refactoring.

Positive case

Pick is used to select the necessary fields of the form:

type FormFields = Pick<User, 'email' | 'name'>;

Changes in User are automatically reflected in the form.

Pros:

  • Guaranteed synchronization of types.

Cons:

  • Need to ensure that no extra fields are selected that are not supported in the business logic of the form.