-
-
Save jokester/e6a4c11f4d2bb22fbbc48b547f49f980 to your computer and use it in GitHub Desktop.
deepFreeze.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type DeepReadonly<T> = | |
T extends (string | number | symbol | boolean) ? T : | |
T extends RegExp ? T : | |
T extends Function ? T : | |
T extends Map<infer K, infer V> ? ReadonlyMap<K, V> : | |
T extends Set<infer E> ? ReadonlySet<E> : | |
T extends (infer E)[] ? ReadonlyArray<Readonly<E>> : // FIXME: can we apply DeepReadonly to collection elements? | |
T extends {} ? { readonly [k in keyof T]: DeepReadonly<T[k]>; } | |
: never; | |
export function deepFreeze<T>(t: T): DeepReadonly<T> { | |
return t as any; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment