TypeScript 7 reference · Utility Types
TypeScript Partial<Type> Utility Type
Make every property of a type optional. Useful for update functions that accept only the fields that change.
TypeScript 7 Since 2.1
Syntax
Partial<Type>
Parameters
| Parameter | Description |
|---|---|
Type |
The object type to transform. |
Return value
A type with the same properties, all optional (?).
Examples
interface User {
name: string;
email: string;
age: number;
}
function update(user: User, changes: Partial<User>): User {
return { ...user, ...changes };
}
const ana: User = { name: "Ana", email: "[email protected]", age: 34 };
console.log(update(ana, { age: 35 }));
Output
{ name: 'Ana', email: '[email protected]', age: 35 }interface Settings {
theme: string;
fontSize: number;
}
const defaults: Settings = { theme: "light", fontSize: 14 };
const saved: Partial<Settings> = { theme: "dark" };
console.log({ ...defaults, ...saved });
// @ts-expect-error: properties still have their types
const wrong: Partial<Settings> = { fontSize: "big" };
Output
{ theme: 'dark', fontSize: 14 }Partial only affects the top level: nested objects keep their required properties. A 'deep partial' needs a recursive mapped type.
Version history
| Version | Change |
|---|---|
| TypeScript 2.1 | Added |
Related
Required<Type>· Make every property of a type required, removing the ? from optional properties. The opposite of Partial.Readonly<Type>· Make every property of a type read-only, so the compiler rejects assignments to them after creation.Pick<Type, Keys>· Build a type from just some properties of another type. Handy for API responses and props that need a subset.- TypeScript Utility Types · tutorial