Skip to content

Instantly share code, notes, and snippets.

@azerum
Created April 21, 2023 16:33
Show Gist options
  • Select an option

  • Save azerum/27c18825eb992301fec8d489a8afa419 to your computer and use it in GitHub Desktop.

Select an option

Save azerum/27c18825eb992301fec8d489a8afa419 to your computer and use it in GitHub Desktop.
export type DeepReadonly<T> =
//Functions are objects, but don't need to be deep readonly
T extends Function ? T :
T extends Map<infer MK, infer MV> ? ReadonlyMap<DeepReadonly<MK>, DeepReadonly<MV>> :
T extends Set<infer E> ? ReadonlySet<DeepReadonly<E>> :
T extends Array<infer E> ? ReadonlyArray<DeepReadonly<E>> :
T extends object ? DeepReadonlyObject<T> :
T;
type DeepReadonlyObject<T> = {
readonly [K in keyof T]: DeepReadonly<T[K]>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment