C.W.K.
Stream
Lesson 02 of 05 · published

`Pick<T, K>` and `Omit<T, K>`

~8 min · utility-types, pick, omit, subset

Level 0Curious
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete
"Pick when you know what to keep. Omit when you know what to drop."

Pick<T, K> — select specific keys

Pick<User, 'id' | 'email'> creates a new type with only those two fields. The second parameter K is constrained to be keyof T, so you can't ask for keys that don't exist. The resulting type has only the picked fields, with their original types.

Useful when you want to expose a subset of an interface — e.g., a public profile that's a subset of the full User shape.

Omit<T, K> — remove specific keys

Omit<User, 'password'> creates a new type without that field. The remaining fields keep their types. Useful when you want everything except a sensitive subset — strip password before returning to a client, strip internal_id before sending to an API.

When to choose which

Both are valid for the same operation; pick whichever has the shorter list. If you want 3 fields out of 20, Pick is more obvious. If you want 17 out of 20, Omit reads better.

Also: Pick fails loudly if you typo a key (the constraint catches it). Omit only fails loudly in strict mode — without it, omitting a non-existent key is silently allowed.

Pick and Omit are inverse operations. Together they let you describe any subset of a type's properties as a transformation of the original. Use them instead of manually retyping subsets — they're refactor-safe.

Code

Pick and Omit, mirror operations·typescript
interface User {
  id: number;
  name: string;
  email: string;
  password: string;
  createdAt: Date;
}

// Pick — select specific keys.
type PublicProfile = Pick<User, 'id' | 'name'>;
// { id: number; name: string }

// Omit — remove specific keys.
type SafeUser = Omit<User, 'password'>;
// { id: number; name: string; email: string; createdAt: Date }

function toPublic(u: User): PublicProfile {
  return { id: u.id, name: u.name };
}

function sanitize(u: User): SafeUser {
  const { password, ...rest } = u;
  return rest;
}

External links

Exercise

Given the User interface above, declare two types: a CreateUserInput that's User without id and createdAt, and a LoginInput that's only email and password. Which uses Pick and which uses Omit? Both are valid for both — explain your choice.
Hint
CreateUserInput → Omit (you want all-minus-two). LoginInput → Pick (you want only two). The choice depends on the size of the list.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue
💛 by Ttoriwarm

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.