API response projections
Expose only an identifier and display name in a list response.
TypeScript · Type Alias
Pick<Type, Keys> creates a type containing only the properties named by Keys from Type. It is available globally and supports a single property name or a union of property names.
Pick constructs a new object type from an existing type by retaining only the properties named by its second type argument. The selected names must be string literals or a union of string literals that identify properties in the source type.
Pick<Type, Keys>| Name | Type | Description |
|---|---|---|
Type | type | The source object type whose properties are candidates for selection. |
Keys | string literal or union of string literals | A string literal or union of string literals naming properties to retain. |
A type containing the selected properties from Type.
type User = {
id: string;
name: string;
email: string;
active: boolean;
};
type UserPreview = Pick<User, "id" | "name">;
const preview: UserPreview = {
id: "u-1",
name: "Ada",
};
console.log(preview);Creates a smaller view of User containing only the selected id and name properties.
Expose only an identifier and display name in a list response.
Define component props from a larger domain model.
Create a lightweight object shape for summaries or previews.
Pick<Type, Keys> creates a type containing only the properties listed by Keys from Type. Keys can be a string literal or a union of string literals.
Follow-up: How would you select several fields? Use a union such as Pick<User, "id" | "name">, which retains both properties.
It is useful when a larger model contains more data than a particular API response or component should expose. The resulting type is derived from the original type rather than rewritten manually.
Follow-up: What maintenance benefit follows? Changes to the selected properties in the source type flow into the picked type.
Pick is available globally as a TypeScript utility type, so it does not require an import. Its documented form is Pick<Type, Keys>.
Follow-up: When was it introduced? The documentation marks Pick as released in TypeScript 2.1.
PartialReadonlyRecordOmit