1 import {Primitive} from './basic';
4 Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
6 This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
8 Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
18 import {ReadonlyDeep} from 'type-fest';
19 import dataJson = require('./data.json');
21 const data: ReadonlyDeep<typeof dataJson> = dataJson;
26 import data from './main';
29 //=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
32 export type ReadonlyDeep<T> = T extends Primitive | ((...arguments: any[]) => unknown)
34 : T extends ReadonlyMap<infer KeyType, infer ValueType>
35 ? ReadonlyMapDeep<KeyType, ValueType>
36 : T extends ReadonlySet<infer ItemType>
37 ? ReadonlySetDeep<ItemType>
39 ? ReadonlyObjectDeep<T>
43 Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
45 interface ReadonlyMapDeep<KeyType, ValueType>
46 extends ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>> {}
49 Same as `ReadonlyDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `ReadonlyDeep`.
51 interface ReadonlySetDeep<ItemType>
52 extends ReadonlySet<ReadonlyDeep<ItemType>> {}
55 Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper for `ReadonlyDeep`.
57 type ReadonlyObjectDeep<ObjectType extends object> = {
58 readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>