One minute
TypeScript: Omit<Type,Keys>
As I am working a lot with TypeScript these days I ran into some interesting TypeScript Utility types that I would like to explore a bit more in a series of small blog posts.
Omit<Type,Keys>
With the Omit utility type we are able to define a new type by taking an existing type and remove one or multiple keys.
example:
type Pizza = {
name: string,
spiciness: number,
price: number
}
We can create a new type of a Pizza without the price with the Omit utility. The result is a type with just the name and spiciness keys.
type PizzaWithoutPrice = Omit<Pizza, "price">;
Creating an object of this type would look something like:
const pizza: PizzaWithoutPrice = {
name: 'Quatro Stagioni',
spiciness: 2
}
You can also take it one step further by removing mutiple keys:
type PizzaWithJustTheName = Omit<Pizza, "spiciness" | "price">;
For a complete overview of TypeScript Utility types please check: