Skip to content

Instantly share code, notes, and snippets.

@jokester jokester/deepFreeze.ts Secret
Last active Jan 25, 2019

Embed
What would you like to do?
deepFreeze.ts
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
You can’t perform that action at this time.