Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created October 1, 2021 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiya000/950c3cb649e0a8742c11c8fc01bdd421 to your computer and use it in GitHub Desktop.
Save aiya000/950c3cb649e0a8742c11c8fc01bdd421 to your computer and use it in GitHub Desktop.
/**
* Exposes type functions.
*/
/**
* [Reference](https://stackoverflow.com/questions/42999983/typescript-removing-readonly-modifier)
*/
export type DeepMutable<T> = {
-readonly [P in keyof T]: DeepMutable<T[P]>;
};
/**
* A deeply `Readonly<T>`.
*/
export type DeepReadonly<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>;
};
/**
* A dual of [[DeepMutable]].
*/
export type DeepImmutable<T> = DeepReadonly<T>;
/**
* A proof to check that [[DeepMutable]] allows assignments.
*/
const _proofDeepMutable = () => {
const _x: DeepMutable<{
readonly xx: {
readonly xxx: number;
};
}> = { xx: { xxx: 42 } };
_x.xx.xxx = 10;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment