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
Can anybody to explain how does this black evil magic work?
typeUnionToIntersection<U>=(Uextendsany ? (k: U)=>void : never)extends((k: infer I)=>void) ? I : never
If I simplify (U extends any ? (k: U) => void : never) -> (a: U)=>void)
it starts to work just as expected (No intersection)
typeUnionToIntersection<U>=((a: U)=>void)extends((k: infer I)=>void) ? I : nevertypeF=UnionToIntersection<'A'|'B'>// A | B
Distributive conditional types iterate over union and apply all union units to this condition
But somehow this condition (U extends any ? (k: U) => void : never) tells ts that we don't need to iterate over union items in the outer extends but in the inner extends and make new type: ((k: 'A') => void) | ((k: 'B') => void)
so after all this combinations ts would work with type like this:
typeH=(((k: 'A')=>void)|((k: 'B')=>void))extends(k: infer I)=>void ? I : never;
And return 'A' & 'B' because function argument is contra-variant
So question why outer extends doesn't iterate union?
The text was updated successfully, but these errors were encountered:
The check type in the outer conditional is (U extends any ? (k: U) => void : never). Conditional types only distribute if the check type is a naked type parameter like U.
Aa, I understand, if we have some calculated type ts doesn't iterate the union
typeJoinReturnType<T>=T&{}extends()=>infer Ret ? ()=>Ret : never;typeA=()=>number;typeB=()=>string;typeU=JoinReturnType<A|B>;// () => string | number// without & {} hack it will be () => string | () => number
Can anybody to explain how does this black evil magic work?
If I simplify
(U extends any ? (k: U) => void : never)
->(a: U)=>void)
it starts to work just as expected (No intersection)
Distributive conditional types iterate over union and apply all union units to this condition
But somehow this condition
(U extends any ? (k: U) => void : never)
tells ts that we don't need to iterate over union items in the outerextends
but in the innerextends
and make new type:((k: 'A') => void) | ((k: 'B') => void)
so after all this combinations ts would work with type like this:
And return
'A' & 'B'
because function argument is contra-variantSo question why outer
extends
doesn't iterate union?The text was updated successfully, but these errors were encountered: