`Omit<Type, Keys>` in TypeScript

Photo by Jules D. on Unsplash

`Omit<Type, Keys>` in TypeScript

Alo's photo
ยทApr 14, 2022ยท

2 min read

This article is part of the Series: Typescript utility types, in plain English

Omit in a nutshell ๐Ÿฅœ

Very similar to the Pick<Type, Keys> utility type, Omit allow you to... You've guessed it, create a new type by omitting certain keys.

Each key is delimited by the 'pipe' operator |

type User = {
    id: number,
    email: string,
    dob: Date

type PublicProfile = Omit<User, 'email'| 'dob'>

Equivalent to

type PublicProfile = {
    firstName: string,
    lastName: string,

Example โœ๏ธ

When should you use Omit?

  1. When you want to hide certain properties

The example above is a good example: a User might want to hide her/his date of birth and email on her/his public profile.

In that case, Omit<Type, Keys> is the way to go.

  1. As a way to combine utility types

As you've noticed, Omit and Pick are pretty much evil twins.

Omit does exactly the opposite of Pick and therefore is also a great 'combinator'.

Omit<Type, Keys> is also an excellent 'combinator'.

type Post = {
    title: string,
    content: string,
    author: User

// This is a pretty useless combinator, but I couldn't come up with an example - COVID is taking a toll on my thinking ability ๐Ÿคฏ
type AnonymousPostTitle = Pick<Omit<Post, 'author'>, 'title'>

Conclusion โ˜๏ธ

Omit<Type, Keys> is simply the opposite of Pick.

Nothing else to report! ๐ŸŽ–๏ธ

When should you use Omit over Pick when selecting some properties out of an existing type? There are no right answers.

Laziness is the key, simply choose the one that is the easiest in the situation.

Share this