What is Pick in TypeScript?
Hemanta Sundaray
Published December 28, 2023
Pick is a built-in utility type in TypeScript. It allows you to create a new type by selecting a subset of properties from an existing type. This is particularly useful when you want to create a type that only needs a few properties from a larger, more complex type.
For example, consider the type below:
type Product = {
name: string;
description: string;
price: number;
inStock: boolean;
};
Let’s say you want a type that only includes the name and price properties of Product. You can use Pick to create this type:
type ProductSummary = Pick<Product, "name" | "price">;
Here, ProductSummary is a new type with only the name and price properties from Product. The type of ProductSummary would be equivalent to:
type ProductSummary = {
name: string;
price: number;
};