Pick vs Omit
Pick<Type, Keys> selects only the given fields from a type, while Omit<Type, Keys> copies every field except the given ones.
Side-by-side comparison
#| Aspect | Pick<Type, Keys> | Omit<Type, Keys> |
|---|---|---|
| What it does | Keeps only the listed fields | Keeps everything except the listed fields |
| Checks whether the keys exist on the type | Yes | No |
| Result for a type with no matching fields | The empty type {} | A copy of the whole source type |
| Reacts to a new field on the source type | Doesnât add it automatically | Adds it automatically |
| Opposite | Omit | Pick |
| Typical use | A public object view, a DTO with a few fields | Form data without the fields generated by the server |
Example
#interface User {
id: number
name: string
passwordHash: string
}
type PublicUser = Pick<User, 'id' | 'name'>
type NewUser = Omit<User, 'id'>
const publicUser: PublicUser = { id: 1, name: 'Emma' }
const newUser: NewUser = { name: 'Jack', passwordHash: 'xyz' }
console.log(publicUser, Object.keys(newUser)){ id: 1, name: 'Emma' } ['name', 'passwordHash']Definition and usage
#Both utility types build a new object type from an existing one, without rewriting it from scratch. They work on the same arguments: a source type and a union of field names.
They differ in which fields remain. Pick<User, "id" | "name"> keeps only the listed fields, and everything else is dropped. Omit<User, "passwordHash"> does the opposite: it keeps everything except the listed fields. Pick checks that the given keys exist on the source type, while Omit doesnât, so a typo in the name of a field youâre removing wonât be reported by the compiler.
Choose Pick when itâs easier to list the few fields you want to keep, for example for a public profile view. Choose Omit when itâs easier to list the few fields to drop, for example form data without the fields generated by the server. You can also combine either one with the & intersection to add a field with a different type.
Syntax
#type New = Pick<Type, "field1" | "field2">
type Other = Omit<Type, "field1">More examples
#interface Task {
id: number
title: string
}
type NewTask = Omit<Task, 'id' | 'ttile'>
const task: NewTask = { title: 'Learning Python' }
console.log(task){ title: 'Learning Python' }The typo ttile doesnât exist on Task, yet the compiler reports nothing, because Omit doesnât check whether the given keys exist on the source type.
interface Task {
id: number
title: string
}
type TaskPreview = Pick<Task, 'id' | 'titel'>
// Error: Type '"titel"' does not satisfy the constraint 'keyof Task'.Good practices
#- When most fields should stay and only a few are dropped, choose
Omit. When only a few fields remain, choosePick. - Types derived with
PickandOmitupdate themselves automatically when the source type changes, so you avoid duplicating definitions. - Combine them with the
&intersection to change the type of one field:Omit<User, "id"> & { id: string }.
Related entries
#- Pick<Type, Keys>Creates a type containing only selected properties of another type.
- Omit<Type, Keys>Creates a type without selected properties of another type.
- Partial<Type>Creates a type where every property is optional.
- Intersection (A & B)A type that combines all the fields of several types at once.
- Record<Keys, Type>Creates an object type with given keys, where every value has the same type.
Spotted a mistake or missing an example? Write to us.