← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 13 of 20

Utility Types - Pick

Pick<T, K> creates a new type containing only selected properties from another type. It is useful when a function or component needs only part of a larger object.

Basic Pick

Pick<T, K> selects one or more properties from an existing type T, given a union of property-name literals K, and produces a smaller type containing just those properties.

Example: Basic Pick

typescript
interface User {
  id: number;
  name: string;
  email: string;
}
type UserPreview = Pick<User, "id" | "name">;
const preview: UserPreview = { id: 1, name: "Ravi" };
console.log(preview);

Pick for Function Arguments

Pick can define a smaller parameter object for functions that only need a few fields from a larger model, instead of requiring the entire original type to be passed in.

Example: Pick for Function Arguments

typescript
interface User {
  id: number;
  name: string;
  email: string;
}
function greet(user: Pick<User, "name">) {
  console.log("Hello", user.name);
}
greet({ name: "Ravi" });

Pick with Interfaces

Pick works with interfaces exactly as it does with type aliases, since both ultimately describe a set of named, typed properties that Pick can select from. This means you can freely mix Pick with either style of type definition without any special-casing.

Example: Pick with Interfaces

typescript
interface Product {
  id: number;
  title: string;
  price: number;
}
type ProductSummary = Pick<Product, "title" | "price">;
const s: ProductSummary = { title: "Book", price: 20 };
console.log(s);

Pick and Type Safety

Pick requires the selected property names to actually exist on the source type, which gives strong compile-time checking against typos in the list of keys you're picking.

Example: Pick and Type Safety

typescript
interface User {
  id: number;
  name: string;
}
type IdOnly = Pick<User, "id">;
const u: IdOnly = { id: 5 };
console.log(u.id);

When to Use Pick

Use Pick when you want a smaller, focused view of an existing type based on only some of its properties, rather than duplicating a subset of fields into a brand-new type definition.

Example: When to Use Pick

typescript
interface Article {
  id: number;
  title: string;
  body: string;
  author: string;
}
type ArticleHeader = Pick<Article, "title" | "author">;
const header: ArticleHeader = { title: "News", author: "Ravi" };
console.log(header);

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.