DeepReadonly<T>
typeDeep readonly type: recursively makes all properties readonly.
The built-in object types are handled before the general mapped-object case, because
mapping over one destroys it.
{ readonly [K in keyof Map<K, V>]: ... } produces an object
carrying the *names* of a Map's methods with their signatures rewritten, so reading a Map
out of state and calling .get() on it was a type error even though the value at runtime
is an ordinary Map. The same applied to Set, Date, RegExp and any function stored in
state.
Collections become their Readonly* counterparts, which is the same treatment arrays
already had. Functions are returned untouched: a function's properties are not state, and
mapping over them makes it uncallable.Definition
type
type DeepReadonly<T> = T extends (args: never[]) => unknown ? T : T extends A[] ? ReadonlyArray<DeepReadonly<A>> : T extends ReadonlyMap<K, V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends ReadonlySet<V> ? ReadonlySet<DeepReadonly<V>> : T extends Date | RegExp | Promise<unknown> | Error ? T : T extends object ? { [K in keyof T]: DeepReadonly<T[K]> } : TT extends (args: never[]) => unknown ? T : T extends A[] ? ReadonlyArray<DeepReadonly<A>> : T extends ReadonlyMap<K, V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends ReadonlySet<V> ? ReadonlySet<DeepReadonly<V>> : T extends Date | RegExp | Promise<unknown> | Error ? T : T extends object ? { [K in keyof T]: DeepReadonly<T[K]> } : T
Type parameters
| Name | Type | Description |
|---|---|---|
T | — | Type to make readonly. |