I'm trying to create a type that will make any array/object deeply read-only. So far I created this type:
type DeepReadonly<G> = G extends Array<infer U> ?
ReadonlyArray<DeepReadonly<U>> :
(G extends object ? Readonly<{[p in keyof G]: DeepReadonly<G[p]>}> : G);
But I have a problem when the generic type is any
.
In this case, DeepReadonly
becomes a union type of all possibilities in the conditional type above including any
.
Which ends up with not read-only type.
For example:
let x: DeepReadonly<any> = 1;
x = 2; // no error
Is there a type like 'any' that can be defined as readonly in case it's array/object? Is it possible to achieve what I want in the first place with TypeScript?
Thanks in advance!