# Pick & Omit

> Create new types by selecting or removing keys.

Subject: TypeScript | Lesson 38 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/pick-and-omit
Previous: [The Record Type](https://codewithanimation.in/typescript/record-type.md) | Next: [Exclude & Extract](https://codewithanimation.in/typescript/exclude-and-extract.md)

## Sub-setting Interfaces

`Pick<Type, Keys>` creates a new type by choosing a specific set of properties from an existing `Type`.

`Omit<Type, Keys>` does the exact opposite: it constructs a new type by taking all properties from `Type` and *removing* the specified `Keys`.

## Single Source of Truth

Instead of manually creating a `UserSummary` interface that duplicates fields from the main `User` interface, you can derive it using `Pick` or `Omit`.

If the type of the `email` field changes in the main `User` interface, the derived types automatically update, preventing drift.

## Syntax

```typescript
interface User {
  id: number;
  name: string;
  email: string;
  passwordHash: string;
}

// Whitelist: We only want these two
type UserSummary = Pick<User, "name" | "email">;

// Blacklist: We want everything EXCEPT these
type SafeUser = Omit<User, "passwordHash">;
```

## Common mistake: Omit's Silent Failure

If you try to `Pick` a key that doesn't exist on the original interface, TypeScript throws an error. This is safe.

However, if you `Omit` a key that *doesn't* exist, TypeScript will silently allow it! `Omit` is based on the `Exclude` utility, which simply ignores keys it can't find. This can lead to bugs if a property name changes and you forget to update the `Omit`.

## Tip: Pick vs Omit

As a general rule, prefer `Pick` (whitelisting) over `Omit` (blacklisting). Whitelisting is safer because you are explicitly stating what data should exist, rather than what shouldn't.

## Try it: Try it

Toggle keys in the source interface and switch between Pick and Omit modes. Watch how the resulting type dynamically updates based on your selection.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/pick-and-omit

## Check yourself

### Question 1

Given `type A = { x: number, y: string }`, what happens if you write `type B = Pick<A, 'z'>`?

- A. B becomes an empty object {}.
- B. TypeScript throws a compile error.
- C. B becomes { z: any }.

Correct answer: B

Pick strictly enforces that the keys provided in the union exist on the source type. Omit, on the other hand, would silently ignore a missing key.

## Remember this

- `Pick` creates a new type containing ONLY the specified keys.
- `Omit` creates a new type containing ALL keys EXCEPT the specified ones.
- `Pick` throws an error if you pass a non-existent key.
- `Omit` silently ignores non-existent keys.
