DEV Community

Cover image for How the TypeScript Omit Type Works
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

5 1

How the TypeScript Omit Type Works

TypeScript provides a number of utility types which are used to solve a particular problem that using types in Javascript creates. One very useful utility type used in TypeScript is the Omit type, which lets us customize an already existing type. Let's look at how it works.

Custom Types

This article assumes you know how to create custom types in TypeScript. If you don't, read my article on custom types here.

TypeScript Omit Type

In TypeScript we often create custom types which let us ensure data conforms to a specific format. For example, if we wanted to create a custom User type which has four fields - firstName, lastName, age and lastActive, we could do something like this:

type User = {
  firstName: string;
  lastName: string;
  age: number;
  lastActive: number;
}
Enter fullscreen mode Exit fullscreen mode

Sadly, coding is not always straight foward. Sometimes, we want to use our type again, but remove certain elements from it, thus creating a new type. To do this, we can use Omit<Type, Omissions>. Omit accepts two values:

  • The Type to base our new type on
  • A Union Type listing all the fields to remove. For example, if we want to take our User type, and remove age and lastActive, we could do the following:
type User = {
  firstName: string;
  lastName: string;
  age: number;
  lastActive: number;
}

type UserNameOnly = Omit<User, "age" | "lastActive">
Enter fullscreen mode Exit fullscreen mode

Now we have two types - User, which is our core user type, and UserNameOnly, which is our User type minus age and lastActive. Similarly, if we only wanted to remove age, this would suffice:

type UserNameAndActive = Omit<User, "age">
Enter fullscreen mode Exit fullscreen mode

Now we can use our new types anywhere in our code. This gives us flexibility to use a type, and transform it for specific circumstances. Here is an example of using both our new types:

type User = {
  firstName: string;
  lastName: string;
  age: number;
  lastActive: number;
}

type UserNameOnly = Omit<User, "age" | "lastActive">
type UserNameAndActive = Omit<User, "age">

const userByName:UserNameOnly = {
    firstName: "John",
    lastName: "Doe",
};
const userWithoutAge:UserNameAndActive = {
    firstName: "John",
    lastName: "Doe",
    lastActive: -16302124725
}
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post