Type Alias: DeepReadonly<T>
ts
type DeepReadonly<T> = T extends (...args) => unknown ? T : T extends readonly infer U[] ? readonly DeepReadonly<U>[] : T extends object ? { readonly [P in keyof T]: DeepReadonly<T[P]> } : T;Defined in: DeepReadonly.ts:19
Utility type that marks every property of a given type as readonly, recursively.
Readonly<T> stops at the top level, so a nested object remains writable and value.nested.field = … still compiles. DeepReadonly applies at every depth, including through arrays.
Functions keep their type, so callbacks on a value remain callable. Primitives keep their type.
Type Parameters
T
T
The type to make deeply readonly.
Example
ts
type PlayerData = {
requestType: string
timing: { start: number }
}
const data: DeepReadonly<PlayerData> = {
requestType: 'segment',
timing: { start: 0 },
}
// Reading at any depth is unchanged.
ok(data.requestType === 'segment')
ok(data.timing.start === 0)
// @ts-expect-error - top-level properties are readonly
data.requestType = 'manifest'
// @ts-expect-error - nested properties are readonly too
data.timing.start = 1