TypeScript utility types: Pick and Omit

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Super happy to hear you are loving them Hossein 💖
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In the previous article, we first looked at TypeScript utility types: Partial and Required.
We'll dive into Pick and Omit in this article.
These are both used to create a new type with only a set of options from the original type.
However, they both work slightly differently. Let's take a look at the high-level difference.
Pick only take the items you define you wantOmit will pick every item you don't define to omitSo the result of both is very similar, it depends on your needs which one you might like.
I'll be sticking to the same example we used before: a user interface.
interface User {
id?: number;
firstname: string;
lastname?: string;
age: number;
telephone?: number;
twitter?: string;
}
Now let's say we want to have a separate type that can pass around only the full name, so it doesn't need any other fields?
We can define a new type in which we can define the fields we would like to use.
type UserFullname = Pick<User, 'firstname' | 'lastname'>;
const userName: UserFullname = {
firstname: 'Chris',
lastname: 'Bongers',
};
Our username variable is now used to ensure only those two fields are set.
You might have spotted the delimiter |. It's used as a separator, and it will select both fields.
You'll often need this kind of type manipulation when using different return types, where you might want to exclude specific fields. But you can also think about child components that only take specific fields from a bigger object.
Like the Pick type, the Omit can be used to modify an existing interface or type.
However, this one works the other way around.
It will remove the fields you defined.
We want to remove the id field from our user object when we want to create a user.
type UserPost = Omit<User, 'id'>;
const updateUser: UserPost = {
firstname: 'Chris',
lastname: 'Bongers',
age: 32,
};
Even though our id was already a conditional field, it's now fully removed from the type, so we can't even pass it along!

And there you have it, the use cases for Pick and Omit in the following article. We'll go more in detail on how powerful they are when combined.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter