avtar

what is `Omit` utility Type

Understanding the "Omit" Utility Type in TypeScript


TypeScript offers a variety of utility types to work with. One of these is the Omit utility type, which allows us to remove certain properties from types.

How does it work ?

The Omit utility type takes a type and properties as arguments, then removes them from the given type. For instance, if you have a type UserResponse:

type UserResponse = {
id: string;
name: string;
age: number;
createdAt: string;
updatedAt: string
}

type UserResponse = {
id: string;
name: string;
age: number;
createdAt: string;
updatedAt: string
}

And you want to remove createdAt and updatedAt, you can use the Omit utility type as follows:

type User = Omit<UserResponse, "createdAT" | "updatedAt">
type User = Omit<UserResponse, "createdAT" | "updatedAt">

Now, createdAt and updatedAt are no longer part of the User type.

I hope this helps clarify the usage of the Omit utility type in TypeScript.