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.
type User = { id: number; name: string }; type UserPatch = Partial<User>; // { id?: number; name?: string }
Try it
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.
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
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
DeepPartialmapped type if you need recursion. - Spread pitfall.
{ ...defaults, ...overrides }copies a property that is present butundefined, overwriting the default. Filter such keys or enableexactOptionalPropertyTypesso thatundefinedcannot 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>andOmit<T, K>choose which keys to keep. - Partial with required keys: to make only some properties optional, combine
OmitandPartial<Pick<...>>, for exampleOmit<T, "id"> & Partial<Pick<T, "id">>. - Available since: TypeScript 2.1, together with mapped types.