DeepReadonly Object Typescript

前端 未结 6 1259
时光说笑
时光说笑 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:58

    You can use ts-toolbelt, it can do operations on types at any depth

    In your case, it would be:

    import {O} from 'ts-toolbelt'
    
    interface A {
      B: { C: number; };
      D: { E: number; }[];
    }
    
    type optional = O.Readonly
    

    And if you want to compute it deeply (for display purposes), you can use Compute for that

提交回复
热议问题