It is possible to create a DeepReadonly
type like this:
type DeepReadonly = {
readonly [P in keyof T]: DeepReadonly;
};
In addition to zenmumbler answer, since TypeScript 3.7 is released, recursive type aliases are now supported and it allows us to improve the solution:
type ImmutablePrimitive = undefined | null | boolean | string | number | Function;
export type Immutable =
T extends ImmutablePrimitive ? T :
T extends Array ? ImmutableArray :
T extends Map ? ImmutableMap :
T extends Set ? ImmutableSet : ImmutableObject;
export type ImmutableArray = ReadonlyArray>;
export type ImmutableMap = ReadonlyMap, Immutable>;
export type ImmutableSet = ReadonlySet>;
export type ImmutableObject = { readonly [K in keyof T]: Immutable };
You may notice that instead of extending the base interfaces, as the old solution does, like interface ImmutableArray
, we refer them directly like type ImmutableArray
.
The old solution works pretty well in most cases, but there are few problems because of replacing original types. For example, if you use immer and pass the old implementation of ImmutableArray
to the produce
function, the draft will lack of array methods like push()
.
There is also the issue on GitHub about adding DeepReadonly type to TypeScript.