Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Utility Types

TypeScript Partial<T> utility type

Reference for TypeScript Partial<T>: make every property optional, use it for updates and defaults, and learn what it does not do (shallow, no deep).

Partial<T> builds a new type with the same properties as T, all of them optional. A value of type Partial<User> may have any subset of the user's properties, including none.

It is most useful for update functions, patches, configuration overrides and builders, where the caller supplies only the fields they want to change.

TypeScript
type User = { id: number; name: string };
type UserPatch = Partial<User>; // { id?: number; name?: string }

Try it

TypeScript Patching an object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Defaults, undefined and shallowness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type Partial<T> = {
  [P in keyof T]?: T[P];
};
Name Description
T Any object type, interface, class instance type or intersection.

Returns: a type with the same keys and the same property types as T, each marked optional with ?. readonly modifiers are preserved.

Edge cases: for arrays and tuples, Partial<[number, string]> yields [number?, string?]. For a union, Partial<A | B> distributes over each member. Partial of a primitive is that primitive.

Usage notes

  • Shallow. Only the top-level properties become optional; nested objects keep their required properties. Define a DeepPartial mapped type if you need recursion.
  • Spread pitfall. { ...defaults, ...overrides } copies a property that is present but undefined, overwriting the default. Filter such keys or enable exactOptionalPropertyTypes so that undefined cannot be written explicitly.
  • Type safety is weaker. The compiler cannot tell which fields are present, so use Partial<T> for inputs, not for the data you store and read back.
  • Inverse: Required<T> removes the ? modifiers; Pick<T, K> and Omit<T, K> choose which keys to keep.
  • Partial with required keys: to make only some properties optional, combine Omit and Partial<Pick<...>>, for example Omit<T, "id"> & Partial<Pick<T, "id">>.
  • Available since: TypeScript 2.1, together with mapped types.