Welcome back! I am Mihir, and in this lesson we will learn the Partial utility type in TypeScript.
Partial<T> makes every property in T optional.
Basic Partial Example
type User = {
id: number;
name: string;
email: string;
};
type PartialUser = Partial<User>;PartialUser behaves like:
type PartialUser = {
id?: number;
name?: string;
email?: string;
};Using Partial
const update: Partial<User> = {
name: "Mihir Soni",
};This is valid because all properties are optional.
You can provide one property, many properties, or no properties.
Update Function Example
function updateUser(user: User, updates: Partial<User>): User {
return {
...user,
...updates,
};
}Usage:
const user: User = {
id: 1,
name: "Mihir",
email: "mihir@example.com",
};
const updatedUser = updateUser(user, {
email: "new@example.com",
});Partial Does Not Change Value Types
const update: Partial<User> = {
id: "one",
};This is invalid because id is still a number.
Partial only makes properties optional.
It does not change their types.
Useful for Forms
type ProfileFormDraft = Partial<{
firstName: string;
lastName: string;
bio: string;
}>;
const draft: ProfileFormDraft = {
firstName: "Mihir",
};Drafts often start incomplete, so Partial fits well.
Quick Recap
Partial<T>makes every property optional.- It is useful for updates, drafts, and patch objects.
- It does not change property value types.
- It works best with object types.
- It helps avoid writing duplicate optional versions of the same type.
Next up, we will learn Required →.