DEV Community

Cover image for TypeScript utility types: Partial and Required
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

20 3 1 1 1

TypeScript utility types: Partial and Required

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.

The TypeScript Partial utility type

Let's take the following example interface to work with.

interface User {
  firstname: string;
  lastname?: string;
  age: number;
}
Enter fullscreen mode Exit fullscreen mode

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);
};
Enter fullscreen mode Exit fullscreen mode

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,
});
Enter fullscreen mode Exit fullscreen mode

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.

TypeScript missing fields error

The TypeScript Required utility type

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;
}
Enter fullscreen mode Exit fullscreen mode

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',
});
Enter fullscreen mode Exit fullscreen mode

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, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (4)

Collapse
 
lexlohr profile image
Alex Lohr

I find it helpful to have a look at how partial is working:

type Partial<T> = {
  [P in keyof T]?: T[P];
};
Enter fullscreen mode Exit fullscreen mode

It just defines a new object type which copies all properties of T as optional.

Collapse
 
navyaraj profile image
navya

got it 👍

Collapse
 
kristiyanvelkov profile image
Kristiyan Velkov

Take your TypeScript skills to new heights with "Mastering TypeScript Core Utility Types":

📖 Buy on Leanpub
📖 Buy on Amazon

Collapse
 
navyaraj profile image
navya

Thank you

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay