Master TypeScript with Advanced Tricks - Partial

Partial Utility Type

The Partial utility type is a predefined TypeScript utility that makes all properties of a given type optional. This can be useful when you need to create a type that allows for partial updates or when you want to provide default values for missing properties.

Let’s start with an example. Consider the following User type:

1
2
3
4
5
6
7
Copy code
interface User {
  id: number;
  name: string;
  email: string;
  age: number;
}

Now, suppose we want to create a function to update a user’s information. The function should accept an object with optional properties, allowing you to update only specific properties. The Partial utility type can help achieve this:

1
2
Copy code
type PartialUser = Partial<User>;

PartialUser will now have the same properties as User, but all of them will be optional:

1
2
3
4
5
6
7
8
Copy code
function updateUser(id: number, updates: PartialUser) {
  // Update the user with the provided information
}

// Usage example:
updateUser(1, { name: "Alice" });
updateUser(2, { email: "bob@example.com", age: 25 });

Cheer! 🍺