Skip to content

55 - Union to Intersection #122

Open

Description

@githubxiaowen

Activity

  1. added
    answer
    en
    on Sep 4, 2020
  2. Sociosarbis commented on Sep 22, 2020

    @Sociosarbis
    type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never
    type I = UnionToIntersection<'foo' | 42 | true> // never
    
    type A = 'foo' | 42 | true
    
    type B = A extends any ? (arg: A) => any : never // (arg: A) => any 
    
    type C = B extends ((arg: infer I) => void) ? I : never //  'foo' | 42 | true
    
    type D = 'foo' & 2 & true // never

    Hi, @githubxiaowen ,I try your solution in my editor.It works when all statements combined as a type template.But When I separate those statements into three statements in order to figure out what mechanism your solution is based on, it didn't work as expect.

    Could you give me more hints to understand the underlying magic of the solution. (I have read the link you attached above.)

    BTW, do you know the reason why the three separated statement doesn't work the same as the template type ?

  3. jo32 commented on Nov 10, 2020

    @jo32
    type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never
    type I = UnionToIntersection<'foo' | 42 | true> // never
    
    type A = 'foo' | 42 | true
    
    type B = A extends any ? (arg: A) => any : never // (arg: A) => any 
    
    type C = B extends ((arg: infer I) => void) ? I : never //  'foo' | 42 | true
    
    type D = 'foo' & 2 & true // never

    Hi, @githubxiaowen ,I try your solution in my editor.It works when all statements combined as a type template.But When I separate those statements into three statements in order to figure out what mechanism your solution is based on, it didn't work as expect.

    Could you give me more hints to understand the underlying magic of the solution. (I have read the link you attached above.)

    BTW, do you know the reason why the three separated statement doesn't work the same as the template type ?

    @Sociosarbis It is because 'foo' & 42 & true is logically never -- you can never find a type which is 42 and true at the same time.

    the magic of this solution is given a type like (arg: U) => any | (arg: V) => any, if you want to find an argument(using keyword infer) satisfying U and V, the type of it must be U & V;

  4. swnb commented on Nov 24, 2020

    @swnb

    amazing!

  5. vogler commented on Mar 4, 2021

    @vogler

    If the intersection for a field is never, you can't provide a value for the object anymore.
    So when using UnionToIntersection on objects out of your control the below StripNever may be useful.

    type t = UnionToIntersection<{a: number, b: number} | {a: number, b: string}>;
    const t1: t = {a: 1}; //  Property 'b' is missing in type '{ a: number; }' but required in type '{ a: number; b: number; }'
    const t2: t = {a: 1, b: 1}; // Type 'number' is not assignable to type 'never'.
    
    type NonNeverKeys<T> = { [K in keyof T]: T[K] extends never ? never : K }[keyof T];
    type StripNever<T> = T extends object ? { [K in NonNeverKeys<T>]: StripNever<T[K]> } : T;
    
    const t3: StripNever<t> = {a: 1}; // ok
  6. Jackie1210 commented on Jul 14, 2021

    @Jackie1210

    image

  7. wine-fall commented on Jan 18, 2022

    @wine-fall

    The solution is amazing, however, i think write as below may be understood more easily

    type UnionToIntersection<U, T = U> =  (U extends T ? (arg: U) => any : never) extends (arg: infer I) => void ? I : never;
    

    And what does the U extends U mean? (when U is an Union Type) you can check link: #614

  8. kstratis commented on Oct 9, 2022

    @kstratis
    type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never
    type I = UnionToIntersection<'foo' | 42 | true> // never
    
    type A = 'foo' | 42 | true
    
    type B = A extends any ? (arg: A) => any : never // (arg: A) => any 
    
    type C = B extends ((arg: infer I) => void) ? I : never //  'foo' | 42 | true
    
    type D = 'foo' & 2 & true // never

    Hi, @githubxiaowen ,I try your solution in my editor.It works when all statements combined as a type template.But When I separate those statements into three statements in order to figure out what mechanism your solution is based on, it didn't work as expect.

    Could you give me more hints to understand the underlying magic of the solution. (I have read the link you attached above.)

    BTW, do you know the reason why the three separated statement doesn't work the same as the template type ?

    I was wondering the exact same thing, so I turned to SO.
    Turns out your break-down is missing generics:
    https://stackoverflow.com/a/74003461/178728

  9. Sociosarbis commented on Oct 9, 2022

    @Sociosarbis
    type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never
    type I = UnionToIntersection<'foo' | 42 | true> // never
    
    type A = 'foo' | 42 | true
    
    type B = A extends any ? (arg: A) => any : never // (arg: A) => any 
    
    type C = B extends ((arg: infer I) => void) ? I : never //  'foo' | 42 | true
    
    type D = 'foo' & 2 & true // never

    Hi, @githubxiaowen ,I try your solution in my editor.It works when all statements combined as a type template.But When I separate those statements into three statements in order to figure out what mechanism your solution is based on, it didn't work as expect.
    Could you give me more hints to understand the underlying magic of the solution. (I have read the link you attached above.)
    BTW, do you know the reason why the three separated statement doesn't work the same as the template type ?

    I was wondering the exact same thing, so I turned to SO. Turns out your break-down is missing generics: https://stackoverflow.com/a/74003461/178728

    an answer with clear explanations. thx @kstratis

  10. quantum-rose commented on Jun 25, 2024

    @quantum-rose

    I got never ts playground

  11. skeleton1231 commented on Aug 22, 2024

    @skeleton1231

    type UnionToIntersection =
    (U extends any ? (x: U) => void : never) extends (x: infer R) => void
    ? R
    : never;

  12. skeleton1231 commented on Aug 27, 2024

    @skeleton1231
    ```ts
    type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never

    Expect<Equal<UnionToIntersection, never>>,
    how about this if single type also regard as union type?

  13. goddnsgit commented on Apr 10, 2025

    @goddnsgit

    我看到了这句话,然后明白了.
    多个函数类型的联合 (A => any) | (B => any),在匹配 (infer I) => any 时,I 必须同时满足所有函数的参数类型,即 I 必须是 A & B。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    55answeren

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions