object-transforms
Partial<T>
Constructs a type where every property of T is set to optional. Useful for representing update payloads, patch objects, and configuration overrides where all fields may be omitted.
type Partial<T> = { [P in keyof T]?: T[P] } Usage patterns
| Pattern | Purpose |
|---|---|
| Partial<User> | Make every top-level field of User optional |
| Partial<Pick<T, K>> | Make only a chosen subset of properties optional |
| Object.assign({}, defaults, partial) | Common runtime companion to merge partial patches |
| DeepPartial<T> | Custom recursive version — Partial itself is not deep |
Examples
interface User { id: number; name: string; email: string }
type PatchUser = Partial<User>; All three fields become optional: { id?; name?; email? }
function update(user: User, patch: Partial<User>): User {
return { ...user, ...patch };
} Classic patch-merge signature accepting any subset of User fields
type Config = { host: string; port: number; ssl: { enabled: boolean } };
const c: Partial<Config> = { ssl: {} }; Errors: nested ssl.enabled is still required because Partial is shallow
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; Selective-optional helper built by composing Partial with Pick/Omit
Gotcha
Partial is shallow — nested object properties keep their required fields. Also weakens type safety: forgetting a field will not error, so pair with runtime validation for API inputs.
Related utility types
Required<T>
Removes optionality from every property of T, producing a type where all fields must be present. The -? mapping modifier strips the ? flag from every key.
Readonly<T>
Marks every property of T as readonly, preventing reassignment after construction. Only enforced at compile-time — the underlying object is still mutable at runtime.
Pick<T, K>
Builds a new type containing only the properties from T whose keys are in the union K. Ideal for narrowing large interfaces to the subset a function or component actually needs.
Omit<T, K>
Constructs a type by removing the properties in K from T. Unlike Pick, its K is not constrained to keyof T, so extra keys are silently ignored.
Record<K, V>
Creates an object type whose keys come from the union K and whose values all have type V. Useful for lookup tables, dictionaries, and enum-keyed maps.
← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never