Sub topic 2
Partial: making every property optional
This chapter assumes you know TypeScript interfaces, type aliases, object types, and generic syntax. You will learn how Partial<T> derives an update type from an existing object type.
Why Partial exists
An interface often describes a complete value:
interface User {
id: number;
name: string;
email: string;
active: boolean;
}A user loaded from a database should contain every required property. An update request is different. It might change only one property:
const changes = {
email: "[email protected]",
};Writing a second interface for every possible update is repetitive. Partial<User> creates a new type in which every property from User is optional.
type UserUpdate = Partial<User>;
const update: UserUpdate = {
email: "[email protected]",
};The property types do not change. email is still a string, and active is still a boolean. The question is only whether each property must be present.
| Original property | Property in `Partial<User>` |
|---|---|
| `name: string` | `name?: string` |
| `active: boolean` | `active?: boolean` |
Using Partial in a function
A common use is a function that applies selected changes to an existing value:
interface User {
id: number;
name: string;
email: string;
active: boolean;
}
function updateUser(user: User, changes: Partial<User>): User {
return {
...user,
...changes,
};
}
const ada: User = {
id: 1,
name: "Ada",
email: "[email protected]",
active: true,
};
const updated = updateUser(ada, {
active: false,
});
console.log(updated);
// { id: 1, name: "Ada", email: "[email protected]", active: false }The spread operator creates a new object. Properties in changes replace properties with the same names in user. The original ada object is not changed.
TypeScript still checks the names and values:
updateUser(ada, { active: "no" });
// Error: Type 'string' is not assignable to type 'boolean'.
updateUser(ada, { username: "ada" });
// Error: 'username' is not a property of User.How Partial works
Partial<User> is a mapped type. A mapped type visits the keys of another type and creates a property for each key. Partial adds the optional modifier, written as ?.
flowchart TD
A["User"] --> B["id: number"]
A --> C["name: string"]
A --> D["email: string"]
B --> E["id?: number"]
C --> F["name?: string"]
D --> G["email?: string"]
E --> H["Partial<User>"]
F --> H
G --> Hcaption: How Partial transforms an object type alt: A User type with three required properties branches into the same three properties, each marked optional, forming Partial of User.
The transformation keeps the original property names and value types:
type User = {
id: number;
name: string;
};
type OptionalUser = Partial<User>;
// {
// id?: number;
// name?: string;
// }An optional property may be absent. When you read it, TypeScript may also treat its value as undefined, so check it before using it where necessary.
function printName(user: Partial<User>): void {
if (user.name !== undefined) {
console.log(user.name.toUpperCase());
}
}
printName({ name: "Ada" });
printName({});A key limitation: Partial is shallow
Partial<T> changes only the top-level properties. It does not recursively make nested properties optional.
interface Settings {
theme: {
color: string;
fontSize: number;
};
}
const changes: Partial<Settings> = {
theme: {
color: "dark",
},
};
// Error: 'fontSize' is missing.Because theme exists in changes, its value must still be a complete theme. A recursive type such as DeepPartial<T> is needed when nested updates are required.
Exercise
What type should you use for a function that accepts any subset of the properties in an existing Product interface?
Show answer
Use Partial<Product>. It keeps each original property type while making every property optional.