How To Use Partial in TypeScript
Here’s how to get optional object properties in TS ☑

When working with TypeScript, sometimes we need to make all properties of an object type optional. This is where the Partial utility type comes into play. It’s a powerful tool that can help us create more flexible and maintainable code. In this article, we’ll explore how to use Partial in TypeScript to effectively make “partial” object types.
What is Partial?
Partial is a utility type provided by TypeScript that constructs a new type with all properties of the given type set to optional. It allows us to represent all possible subsets of a given type. Here’s the definition of Partial:
type Partial<T> = {
[P in keyof T]?: T[P];
}Using Partial
Let’s consider a simple example of using Partial. Suppose we have a type Todo that represents a to-do item, the classic frontend example:
type Todo = {
title: string;
description: string;
}Now, let’s say we want to create a function that updates a Todo object by applying a set of updates. We could use the Partial utility type to make all the properties of Todo optional in the fieldsToUpdate parameter:
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
return { ...todo, ...fieldsToUpdate };
}
const todo1 = {
title: "organize desk",
description: "clear clutter",
}
const todo2 = updateTodo(todo1, {
description: "throw out trash",
})In this example, the updateTodo function takes a Todo object and a Partial<Todo> object as its parameters. The Partial<Todo> object allows us to update only a subset of properties from the original Todo object.
By the way, if you need a quick refresher on the ... syntax used in the code above, I’ve written 2 articles on that subject you should check out:
Advanced Use Case: DeepPartial
While Partial works great for simple objects, it doesn’t handle nested objects as you might expect. For example, let’s consider a slightly more complex Todo interface with a nested object:
interface Todo {
title: string;
metadata: {
createdAt: Date;
updatedAt: Date;
};
}If we try to use Partial<Todo> here, only the top-level properties will become optional, not the properties of the nested metadata object. To make all properties optional recursively, we can create a custom DeepPartial utility type:
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
}Now, if we use DeepPartial<Todo>, all nested properties will also become optional:
function updateDeepTodo(todo: Todo, fieldsToUpdate: DeepPartial<Todo>) {
// Implementation
}
const todo3 = {
title: "finish report",
metadata: {
createdAt: new Date(),
updatedAt: new Date(),
},
}
const todo4 = updateDeepTodo(todo3, {
metadata: {
updatedAt: new Date(),
},
})With DeepPartial, we can now update any level of properties within the Todo object. Hopefully, you can start to see how this can be useful in TS: any valid subtype of the deeply-nested object is a valid DeepPartial.
Conclusion: Partial Utility Type in TypeScript ☑
The Partial utility type in TypeScript is an invaluable tool for making all properties of a type optional. It helps us create more flexible and maintainable code by allowing us to represent all possible subsets of a given type. And with the help of custom utility types like DeepPartial, we can extend its capabilities to handle even more complex scenarios. Embrace the optional properties and start using Partial in your TypeScript projects today to make handling object types so much easier.
Happy coding! ☑️







