How to implement TypeScript deep partial mapped type not breaking array properties

前端 未结 4 2143
遇见更好的自我
遇见更好的自我 2020-12-24 01:16

Any ideas as to how might apply TypeScript\'s Partial mapped type to an interface recursively, at the same time not breaking any keys with array return types?

The fo

4条回答
  •  爱一瞬间的悲伤
    2020-12-24 01:55

    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 User {  
        emailAddress: string;  
        verification: {
          verified: boolean;
          verificationCode: string;
        }
        activeApps: string[];
    }
    
    type optional = O.Optional
    

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

提交回复
热议问题