TypeScript utility types: Partial and Required

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.
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...

TypeScript comes with several utility types. These are utilities we can use to do type transformations.
We'll look at the Partial and Required types in this article.
To give you a bit more context, you might have written an interface or type that reflects a user object, but in some cases, you want to use only certain fields or change which fields are required from this interface.
And that's precisely where the utility types come in handy, there is a whole set of them, and I'll be going through the most commonly used ones.
Let's take the following example interface to work with.
interface User {
firstname: string;
lastname?: string;
age: number;
}
As you can see, we made two fields required: firstname and age. The lastname field is optional because we added the ? to it.
However, what if we have an update where we would allow all of the fields to be optional valid?
This could, for instance, be if we have a UI where each field will auto-update without knowing any of the other fields.
Our function for this could be updateUserField, which would accept any user fields.
const updateUserField = (id: number, fieldsToUpdate: Partial<User>) => {
return db.update(id, fieldsToUpdate);
};
And we can now use this to update each field individually without needing the other ones to be set.
updateUserField(1, {
firstname: 'Chris',
});
updateUserField(1, {
age: 32,
});
This is now valid code. However, if you would remove the Partial utility, you would see it throws some TypeScript errors about the missing fields.

On the other hand, there might be cases where you expect the value to be set.
Let's look at our user example again. By default, you might have an object where the ID is not required since we don't know it yet, but once the user is created, we could set it to be required.
interface User {
id?: number;
firstname: string;
lastname: string;
}
We can use this User interface without specifying the ID when creating the user.
But when we want to update the user, we want to make sure the ID is set.
const updateUser = (user: Required<User>) => {
db.update(user);
};
updateUser({
id: 12,
firstname: 'Chris',
lastname: 'Bongers',
});
Because of the Required type, every field in the User interface is now required.
A cool thing with TypeScript utility types is that you can also mix and match them.
In a different article, we'll look at how we can only make specific fields required or optional by leveraging some other utility types.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter