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.
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.
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.
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:
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.
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:
Cons:
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:
Cons: