Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Utility Types

TypeScript Pick<T, K> utility type

Reference for TypeScript Pick<T, K>: build a smaller object type that keeps only the chosen properties, with examples, constraints and pitfalls.

Pick<T, K> creates a type containing only the properties of T whose names are listed in K. The kept properties keep their types, optional markers and readonly flags. Use it to describe "just these fields" without redefining them.

It is handy for function parameters that need a few fields of a large object, and for public views of an internal type. The opposite operation is Omit<T, K>.

TypeScript
interface User {
  id: number;
  name: string;
  email: string;
  password: string;
}

type PublicUser = Pick<User, "id" | "name">;
// { id: number; name: string }

Try it

TypeScript A public view of a type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Only existing keys are allowed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
};
Name Description
T The source object type.
K A key or a union of keys of T. The constraint K extends keyof T makes unknown names a compile error.

Result: a mapped type with just the keys in K. It is a homomorphic mapped type, so modifiers (?, readonly) are copied from T.

Edge cases: Pick<T, never> is {}. For a union T, Pick does not distribute; use a distributive helper if you need that.

Usage notes

  • Modifiers are kept. Optional and readonly properties stay that way in the result.
  • Typos are caught. Because of the keyof T constraint, Pick<User, "emial"> is an error.
  • Stays in sync. If the type of User["name"] changes, PublicUser changes with it. A hand-copied interface would not.
  • Not deep. Only top-level properties can be picked. For nested pieces, use indexed access (User["address"]["city"]).
  • Unions. Pick<A | B, K> requires K to be keys of the union (present in both) and returns a single merged object type, not a union. Write T extends unknown ? Pick<T, K> : never to pick per member.
  • Combining: Pick<T, K> & Partial<Omit<T, K>> makes some keys required and the rest optional.
  • Runtime: it only exists in the type system. Copy the fields yourself (destructuring or an explicit object) to actually drop data.
  • Available since TypeScript 2.1.