You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeUnionToIntersection<U>=(Uextendsany ? (arg: U)=>any : never)extends((arg: infer I)=>void) ? I : nevertypeI=UnionToIntersection<'foo'|42|true>// nevertypeA='foo'|42|truetypeB=Aextendsany ? (arg: A)=>any : never// (arg: A) => any typeC=Bextends((arg: infer I)=>void) ? I : never// 'foo' | 42 | truetypeD='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 ?
typeUnionToIntersection<U>=(Uextendsany ? (arg: U)=>any : never)extends((arg: infer I)=>void) ? I : nevertypeI=UnionToIntersection<'foo'|42|true>// nevertypeA='foo'|42|truetypeB=Aextendsany ? (arg: A)=>any : never// (arg: A) => any typeC=Bextends((arg: infer I)=>void) ? I : never// 'foo' | 42 | truetypeD='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;
Reacted by Yorke, ZGY, zheyaoa, Olzhas Seitmagambet, bangkk5, Nemo, David Tai, dive2Pro, kazusa, Sun and 14 more
Reacted by ZGY, David Tai and Konstantinos Stratis
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.
typet=UnionToIntersection<{a: number,b: number}|{a: number,b: string}>;constt1: t={a: 1};// Property 'b' is missing in type '{ a: number; }' but required in type '{ a: number; b: number; }'constt2: t={a: 1,b: 1};// Type 'number' is not assignable to type 'never'.typeNonNeverKeys<T>={[KinkeyofT]: T[K]extendsnever ? never : K}[keyofT];typeStripNever<T>=Textendsobject ? {[KinNonNeverKeys<T>]: StripNever<T[K]>} : T;constt3: StripNever<t>={a: 1};// ok
typeUnionToIntersection<U>=(Uextendsany ? (arg: U)=>any : never)extends((arg: infer I)=>void) ? I : nevertypeI=UnionToIntersection<'foo'|42|true>// nevertypeA='foo'|42|truetypeB=Aextendsany ? (arg: A)=>any : never// (arg: A) => any typeC=Bextends((arg: infer I)=>void) ? I : never// 'foo' | 42 | truetypeD='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 ?
typeUnionToIntersection<U>=(Uextendsany ? (arg: U)=>any : never)extends((arg: infer I)=>void) ? I : nevertypeI=UnionToIntersection<'foo'|42|true>// nevertypeA='foo'|42|truetypeB=Aextendsany ? (arg: A)=>any : never// (arg: A) => any typeC=Bextends((arg: infer I)=>void) ? I : never// 'foo' | 42 | truetypeD='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 ?
Activity
Sociosarbis commentedon Sep 22, 2020
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 ?
jo32 commentedon Nov 10, 2020
@Sociosarbis It is because
'foo' & 42 & trueis logicallynever-- you can never find a type which is42andtrueat 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;swnb commentedon Nov 24, 2020
amazing!
vogler commentedon Mar 4, 2021
If the intersection for a field is never, you can't provide a value for the object anymore.
So when using
UnionToIntersectionon objects out of your control the belowStripNevermay be useful.Jackie1210 commentedon Jul 14, 2021
wine-fall commentedon Jan 18, 2022
The solution is amazing, however, i think write as below may be understood more easily
And what does the
U extends Umean? (whenUis an Union Type) you can check link: #614kstratis commentedon Oct 9, 2022
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
Sociosarbis commentedon Oct 9, 2022
an answer with clear explanations. thx @kstratis
quantum-rose commentedon Jun 25, 2024
I got never ts playground
skeleton1231 commentedon Aug 22, 2024
type UnionToIntersection =
(U extends any ? (x: U) => void : never) extends (x: infer R) => void
? R
: never;
skeleton1231 commentedon Aug 27, 2024
Expect<Equal<UnionToIntersection, never>>,
how about this if single type also regard as union type?
goddnsgit commentedon Apr 10, 2025
我看到了这句话,然后明白了.
多个函数类型的联合 (A => any) | (B => any),在匹配 (infer I) => any 时,I 必须同时满足所有函数的参数类型,即 I 必须是 A & B。