Sub topic 9
Recursive Mapped Types: Building `DeepReadonly`
This chapter assumes you know TypeScript generics, conditional types, unions, function signatures, keyof, and basic mapped types. We will combine these features to make a type that recursively marks every property in a structure as readonly.
Why a shallow type is not enough
TypeScript's built-in Readonly<T> changes only the top-level properties:
type Settings = {
server: {
host: string;
ports: number[];
};
};
type ShallowSettings = Readonly<Settings>;With ShallowSettings, you cannot replace server, but you can still change server.host and call server.ports.push(...). The nested values remain mutable.
A recursive mapped type applies the same transformation at every nested level. DeepReadonly<T> needs to decide what to do with each kind of input:
| Input | Result |
|---|---|
| Function | Keep the function type unchanged |
| Array or tuple | Make the collection readonly and recurse into its elements |
| Object | Make each property readonly and recurse |
| Primitive | Return the type unchanged |
Combining conditional and mapped types
A conditional type chooses a branch with T extends Pattern ? Yes : No. A mapped type visits every property with [K in keyof T].
type DeepReadonly<T> =
T extends (...args: any[]) => any
? T
: T extends readonly unknown[]
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T extends object
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T;
type Config = {
server: {
host: string;
ports: number[];
};
log: (message: string) => void;
};
const config: DeepReadonly<Config> = {
server: {
host: "localhost",
ports: [3000, 3001],
},
log: (message) => console.log(message),
};
config.log("ready");
// @ts-expect-error: server is deeply readonly
config.server.host = "example.com";
// @ts-expect-error: ports is a readonly array
config.server.ports.push(8080);The first branch protects functions from being treated like ordinary objects. A function is callable, so recursively mapping its properties would produce an unusable function type.
The second branch handles arrays and tuples. The readonly mapped modifier changes an array-like type into a readonly version. DeepReadonly<T[K]> then processes each element type.
The third branch handles normal objects. For each key K, it creates a readonly property and recursively transforms that property's value.
The final branch is the base case. Strings, numbers, booleans, null, and other non-object values do not contain properties that need transforming, so the type returns them unchanged.
Follow the recursion
For Config, TypeScript evaluates the structure level by level:
A recursive type must have a base case. Here, the primitive branch stops recursion. Without a branch that eventually stops matching, the compiler would keep expanding the type until it reached a compiler limit.
Important edge cases
“Object” in TypeScript includes values such as Date, Map, class instances, and regular expressions. The generic implementation above maps their visible properties, but it does not make their internal behavior immutable. A Readonly<Date> can still call methods such as setTime.
For a public utility type, decide which structures it supports. You might add special branches for Date, Map, Set, or application-specific classes. You might also choose to preserve branded types or exclude certain built-in objects.
The function branch is another deliberate design choice. It keeps call signatures usable, but it does not recursively freeze properties attached to a function object. Most configuration types do not need to support that case.
Exercise
Which branch acts as the base case in DeepReadonly, and why is a base case necessary?
Show answer
The final branch returns T for non-object values such as strings and numbers. It stops recursion when there are no nested properties to transform.