DeepReadonly Object Typescript

前端 未结 6 1261
时光说笑
时光说笑 2020-12-06 04:50

It is possible to create a DeepReadonly type like this:

type DeepReadonly = {
  readonly [P in keyof T]: DeepReadonly;
};

         


        
6条回答
  •  时光取名叫无心
    2020-12-06 04:55

    In addition to zenmumbler answer, since TypeScript 3.7 is released, recursive type aliases are now supported and it allows us to improve the solution:

    type ImmutablePrimitive = undefined | null | boolean | string | number | Function;
    
    export type Immutable =
        T extends ImmutablePrimitive ? T :
        T extends Array ? ImmutableArray :
        T extends Map ? ImmutableMap :
        T extends Set ? ImmutableSet : ImmutableObject;
    
    export type ImmutableArray = ReadonlyArray>;
    export type ImmutableMap = ReadonlyMap, Immutable>;
    export type ImmutableSet = ReadonlySet>;
    export type ImmutableObject = { readonly [K in keyof T]: Immutable };
    

    You may notice that instead of extending the base interfaces, as the old solution does, like interface ImmutableArray extends ReadonlyArray> {}, we refer them directly like type ImmutableArray = ReadonlyArray>.

    The old solution works pretty well in most cases, but there are few problems because of replacing original types. For example, if you use immer and pass the old implementation of ImmutableArray to the produce function, the draft will lack of array methods like push().

    There is also the issue on GitHub about adding DeepReadonly type to TypeScript.

提交回复
热议问题