When building applications with TypeScript, you often need to create a new type that excludes some properties from an existing type.
Instead of manually rewriting the type, TypeScript provides utilities like Omit
to make this process much easier.
In this guide, you’ll learn how to omit multiple keys from a type, how to exclude keys dynamically, and the key differences between Omit, Exclude, and Pick, all explained clearly and simply.
The Omit
utility type helps you create a new type by removing one or more keys from an existing type.
It’s especially useful when you want to reuse most of a type but without certain properties.
The syntax for Omit
looks like this:
Omit<Type, Keys>
Here, Type
is the original object type, and Keys
are the names of the properties you want to exclude.
Example: Omit a Single Key
Suppose you have a User
type:
type User = {
id: number;
name: string;
email: string;
password: string;
};
If you want a version of User
without the password
field, you can do this:
type PublicUser = Omit<User, 'password'>;
Now, PublicUser
will only include id
, name
, and email
, without password
.
If you need to remove several fields at once, you can simply pass a union of keys to Omit
.
Example: Omit Multiple Fields
Let’s extend the previous example:
type User = {
id: number;
name: string;
email: string;
password: string;
createdAt: Date;
};
If you want to exclude both password
and createdAt
, you can do:
type PublicUser = Omit<User, 'password' | 'createdAt'>;
This way, PublicUser
will include only id
, name
, and email
.
You can list as many fields as needed inside the union using the |
symbol.
Sometimes, you may want to exclude keys dynamically without hardcoding everything inside Omit
.
In these cases, you can combine keyof
, Exclude
, and Pick
.
Let’s go through an example:
type Product = {
id: string;
name: string;
price: number;
cost: number;
supplier: string;
};
type KeysToRemove = 'cost' | 'supplier';
// Step 1: Get all keys from Product
type AllKeys = keyof Product; // 'id' | 'name' | 'price' | 'cost' | 'supplier'
// Step 2: Exclude the unwanted keys
type PublicKeys = Exclude<AllKeys, KeysToRemove>; // 'id' | 'name' | 'price'
// Step 3: Pick only the remaining keys
type PublicProduct = Pick<Product, PublicKeys>;
// Reslut: { id: string, name: string, price: string }
You can also shorten it into one line like this:
type PublicProduct = Pick<Product, Exclude<keyof Product, 'cost' | 'supplier'>>;
This approach is helpful when you want more flexibility or when you want to create reusable utilities.
Although they sound similar, Omit
and Exclude
do two different things.
Omit
removes properties from an object type.Exclude
removes types from a union.
Think of Omit
as working on object shapes, and Exclude
as working on simple types or keys.
For example, if you have a union type:
type Keys = 'id' | 'name' | 'password';
And you want to remove password
, you would use:
type PublicKeys = Exclude<Keys, 'password'>;
// Result: 'id' | 'name'
However, if you want to remove a field from an object type, you should use Omit
:
type User = {
id: number;
name: string;
password: string;
};
type PublicUser = Omit<User, 'password'>;
// Result: { id: number; name: string }
Each utility serves a different purpose, and knowing when to use each makes your TypeScript code cleaner and more maintainable.
Another common question is whether you should use Omit
or Pick
.
Both are useful but serve opposite goals.
- Use
Omit
when you want to remove specific fields from a type. - Use
Pick
when you want to select only certain fields from a type.
For example, if you have a User
type:
type User = {
id: number;
name: string;
email: string;
};
And you want a minimal version with only id
and name
, you would use Pick
:
type MinimalUser = Pick<User, 'id' | 'name'>;
In contrast, if you wanted everything except email
, you would use:
type MinimalUser = Omit<User, 'email'>;
Choosing between Omit and Pick simply depends on whether you’re thinking in terms of excluding or including fields.
In TypeScript, using Omit
to remove multiple properties from a type is straightforward and powerful.
By passing a union of keys, you can easily omit several fields at once.
When you need more flexibility, combining keyof
, Exclude
, and Pick
gives you full control over which keys to keep or remove.
Understanding the difference between Omit
, Exclude
, and Pick
helps you write more readable, reusable, and safer TypeScript code.
Once you master these patterns, you’ll find it much easier to manage types in any size project.
If you enjoyed this article, I’d truly appreciate it if you could share it—it really motivates me to keep creating more helpful content!
If you’re interested in exploring more, check out these articles.
- 4 Ways To Handle Asynchronous JavaScript
- Overloading vs Overriding in TypeScript
- How to Use Discriminated Unions in TypeScript?
And to learn more, please check the TypeScript Documentation:
Thanks for sticking with me until the end—I hope you found this article valuable and enjoyable!
Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.