Created
June 17, 2021 16:53
-
-
Save P4/278939c76c586a7f5d7c298f09eb7980 to your computer and use it in GitHub Desktop.
Make a TypeScript type readonly, recursively
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
type AnyFunction = (...args: any[]) => any; | |
type ImmutablePrimitive = undefined | null | boolean | string | number | AnyFunction; | |
type Immutable<T> = | |
T extends ImmutablePrimitive ? T : | |
T extends Map<infer K, infer V> ? ReadonlyMap<Immutable<K>, Immutable<V>> : | |
T extends Set<infer E> ? ReadonlySet<Immutable<E>> : | |
{ readonly [P in keyof T]: Immutable<T[P]> } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from this comment, including a fix for tuples