Omit<Type, Keys>
How To Use Omit in TypeScript
Understanding the built-in Omit type, which creates new types based on existing ones, but without some of their properties.
In TypeScript, the built-in Omit type is used to create a new type that excludes specific properties from an existing type. It accepts two parameters: the original type and the keys (property names) to omit.
In other words, the Omit utility type creates new types based on existing ones, but without some of their properties. This can be useful in situations where you want to ensure that certain properties are not included in an object, or where you want to create a more restricted version of a type.
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>The Omit type works by combining Pick and Exclude. Exclude is another utility type that creates a new type by excluding certain types from a union. Now we’ll go through a few examples to illustrate how Omit works.
Omitting a Single Property From an Object
So how do you use Omit<Type, Keys> in TypeScript to make a new type that excludes some properties? Let’s look at a mapache-themed example. 🦝
type Mapache = {
name: string;
age: number;
emoji: string;
}
type AgelessMapache = Omit<Mapache, 'age'>
const ricky: AgelessMapache = { name: 'Ricky', emoji: '🦝' };
console.log(ricky) // Output: { name: 'Ricky', emoji: '🦝' }In the above example, we used the Omit utility to create a new type AgelessMapache that is the same as Mapache but without the age property.
Omitting Multiple Properties From an Object
What if you need to omit multiple properties from an object at the same time? You’ll use the | (pipe) character, like you use for string union types.
type NamelessAndAgelessMapache = Omit<Mapache, 'name' | 'age'>
const mysteryMapache: NamelessAndAgelessMapache = { emoji: '🦝' }
console.log(mysteryMapache) // Output: { emoji: '🦝' }This time, we omitted the name and age properties from the Mapache type. That left us with a type that only includes the emoji property.
Conclusion: TypeScript’s Built-In Omit Type
The Omit utility type in TypeScript is a powerful tool that allows you to construct a new type by removing specific properties from an existing one. Its syntax is straightforward, and it provides a clean and intuitive way to make new subtypes from existing types in your TypeScript codebase.
By using Omit, you can create stricter types for specific use-cases, hide sensitive data properties, or simplify complex types by removing irrelevant properties. As such, Omit is a valuable addition to your TS utility belt.
We’ll wrap up with a final TypeScript code example using Omit. If you’d like to try out these code examples, I’ve uploaded them to TS Playground.
// type AgelessMapache = Omit<Mapache, 'age'>
function introduceMapache(mapache: AgelessMapache) {
console.log(`Meet ${mapache.name}, they look like: ${mapache.emoji}`)
}
const micky: AgelessMapache = { name: 'Micky', emoji: '🐭' }
introduceMapache(ricky) // Output: "Meet Ricky, they look like: 🦝"
introduceMapache(micky) // Output: "Meet Micky, they look like: 🐭"In this example, the introduceMapache function only requires a name and emoji for a Mapache. By using Omit, we ensure that we can safely pass a Mapache object to the function without revealing its age property.
Understanding and properly using TypeScript's utility types such as Omit will level up your TypeScript coding skills and allow you to write cleaner, safer, and more predictable code.
Want to go to the source? Read the TypeScript Docs for Omit.
Happy coding! 🚀
