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
#What it does
- Pick<Type, Keys>
- Keeps only the listed fields
- Omit<Type, Keys>
- Keeps everything except the listed fields
Checks whether the keys exist on the type
Result for a type with no matching fields
- Pick<Type, Keys>
- The empty type
{} - Omit<Type, Keys>
- A copy of the whole source type
Reacts to a new field on the source type
- Pick<Type, Keys>
- Doesn’t add it automatically
- Omit<Type, Keys>
- Adds it automatically
Opposite
- Pick<Type, Keys>
Omit- Omit<Type, Keys>
Pick
Typical use
- Pick<Type, Keys>
- A public object view, a DTO with a few fields
- Omit<Type, Keys>
- 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.
- 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.