Intersection of mapped types

前端 未结 2 1301
轻奢々
轻奢々 2020-12-18 06:38

Consider the following:

type Properties = {
    foo: { n: number };
    bar: { s: string };
    baz: { b: boolean };
};

declare function retrieveValues

        
2条回答
  •  醉梦人生
    2020-12-18 07:35

    Using conditional types and type inference in conditional types it is possible to transform { n: number } | { s: string } directly into { n: number } & { s: string }.

    type GetKeys = U extends Record ? K : never
    
    type UnionToIntersection = {
       [K in GetKeys]: U extends Record ? T : never
    }
    
    type Transformed = UnionToIntersection<{ a: string } | { b: number }>
    // Transformed has type {a: string, b: number}
    

    Playground Link

    The reason this works is basically because conditional types are distributed over union types. From the conditional types pull request:

    Conditional types in which the checked type is a naked type parameter are called distributive conditional types. Distributive conditional types are automatically distributed over union types during instantiation. For example, an instantiation of T extends U ? X : Y with the type argument A | B | C for T is resolved as (A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y).

提交回复
热议问题