The TypeScript Omit utility type

Kenny DuMez
Kenny DuMez
Graphite software engineer

TypeScript's Omit type is a utility that allows developers to create new types by excluding specific properties from an existing type. This utility is particularly useful for creating types that are subsets of other types, but with fewer properties.

This guide will explore how to effectively use Omit in TypeScript, including handling multiple properties and applying Omit to various use cases like object types and class properties.

The Omit type is a utility in TypeScript that constructs a new type by picking all properties from an existing type but excluding a set of keys. The syntax for Omit is:

Terminal
Omit<Type, Keys>
  • Type is the original type you are modifying.
  • Keys is the property or properties to omit from the Type.

Here’s a step-by-step process on how to use Omit in various scenarios:

  1. Omitting a single property:

    • Suppose you have a type Person and you want to create a new type that excludes the age property:

      Terminal
      type Person = {
      name: string
      age: number
      email: string
      }
      type PersonWithoutAge = Omit<Person, 'age'>
    • PersonWithoutAge will have only the name and email properties.

  2. Omitting multiple properties:

    • If you need to omit more than one property, you can list them in the Omit utility separated by a comma within the type literal:

      Terminal
      type PersonWithoutAgeAndEmail = Omit<Person, 'age' | 'email'>
    • This will create a type that includes only the name property.

  3. Combining Partial and Omit:

    • Sometimes, you might want to create a type where some properties are optional and some are omitted entirely. This can be achieved by combining Partial and Omit:

      Terminal
      type PartialPerson = Partial<Omit<Person, 'age'>>
    • This type makes name and email optional but completely omits age.

  4. Omitting properties from objects in functions:

    • Omit can be particularly useful in functions where you want to ensure certain object properties are not passed:

      Terminal
      function registerPerson(person: Omit<Person, 'age'>) {
      // function body
      }
    • This function will accept an object of type Person but without the age property.

  5. Omitting class properties:

    • When working with classes, you can use Omit to define types that are used for instance creation without certain properties:

      Terminal
      class Person {
      name: string
      age: number
      email: string
      constructor(data: Omit<Person, 'age'>) {
      this.name = data.name
      this.email = data.email
      // age is omitted
      }
      }
    • This constructor allows creating a Person object without the age property.

  • Form handling: Use Omit to exclude sensitive properties that should not be manipulated through forms.
  • API responses: When fetching data from an API, use Omit to exclude properties that are not relevant or sensitive.
  • Component props: In React or other UI libraries, use Omit to exclude certain props from being passed to a component.

For further reading on Omit in TypeScript see the official documentation.

On this page
Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2