TypeScript Reference ยท Utility Types
Custom helper types in TypeScript (DeepPartial and more)
Write your own TypeScript helper types: DeepPartial, DeepReadonly, Mutable, RequireKeys, ValueOf, UnionToIntersection and Prettify, each explained with runnable code.
The built-in utility types cover the common cases, but real projects keep a small file of their own helpers for the rest. None of these are in the standard library; you write them with mapped types, conditional types and infer, and copy them between projects.
This page collects the ones that come up most often, shows how each is built, and says where it breaks. They are all type-level: they produce no JavaScript, so you test them by assigning values and by checking that wrong code fails to compile.
Mapped helpers
The shallow built-ins (Partial, Readonly) only touch the first level. A recursive mapped type reaches the whole tree.
type DeepPartial<T> = T extends (infer U)[] ? DeepPartial<U>[] : T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T; type DeepReadonly<T> = T extends (infer U)[] ? readonly DeepReadonly<U>[] : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]> } : T; type Mutable<T> = { -readonly [K in keyof T]: T[K] };
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.
Reshaping object types
RequireKeys makes some optional properties required, Prettify flattens an intersection so hover text and error messages are readable, and ValueOf is the value-side twin of keyof.
type RequireKeys<T, K extends keyof T> = T & { [P in K]-?: T[P] }; type Prettify<T> = { [K in keyof T]: T[K] } & {}; type ValueOf<T> = T[keyof T];
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.
Working with unions
UnionToIntersection turns A | B into A & B using the contravariant position of a function parameter. It is the building block for several other tricks.
type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
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.
Usage notes
- Put helpers in one module (
types/helpers.ts) and export them withexport type, so they vanish from the compiled output. - Recursive helpers like
DeepPartialtreat functions,Date,MapandSetas objects and will map over their members, which is rarely what you want. Add branches for them (T extends Function | Date ? T : ...) when your data includes them. Prettifycosts nothing at runtime but makes error messages and editor tooltips much easier to read; the trailing& {}forces the compiler to expand the type.UnionToIntersectioncan collapse toneverwhen members have conflicting property types.- Very deep recursion triggers "type instantiation is excessively deep" errors; keep helpers small, and use tail-recursive forms for long tuples.
- Libraries such as
type-festship tested versions of these helpers if you prefer not to maintain your own.