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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Partial<Type>

Parameters

ParameterDescription
Type The object type to transform.

Return value

A type with the same properties, all optional (?).

Examples

Update only some fieldsTry it yourself
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 }
Merge saved settings over defaultsTry it yourself
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

VersionChange
TypeScript 2.1Added

Was this page helpful?