Typescript 2.8: Remove properties in one type from another

前端 未结 1 1903
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 04:52

In the changelog of 2.8, they have this example for conditional types:

type Diff = T extends U ? never : T;  // Remove types from T that are assi         


        
相关标签:
1条回答
  • 2021-01-19 05:36

    Well, leveraging your Diff type from earlier (which by the way is the same as the Exclude type which is now part of the standard library), you can write:

    type ObjectDiff<T, U> = Pick<T, Diff<keyof T, keyof U>>;
    type AWithoutStuff = ObjectDiff<A, Stuff>; // inferred as { one: string; two: number; }
    
    0 讨论(0)
提交回复
热议问题