-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
730 - Union to Tuple #737
Comments
Could you please add some explanation for the questions below? type h = ((x: 1) => 0) & ((x: 2) => 0) //why h not never type e = (((x: 1) => 0) & ((x: 2) => 0)) extends (x: infer L) => 0 ? L : never; // why e is 2 not never or 1? It stops me from understanding the answer. |
type h = ((x: 1) => 0) & ((x: 2) => 0) //why h not never Function arguments are in contravariant positions, so when functions intersect, arguments do not intersect, but are united. type e = (((x: 1) => 0) & ((x: 2) => 0)) extends (x: infer L) => 0 ? L : never; // why e is 2 not never or 1? This is a feature of TS, mentioned somewhere in the documentation -- if it is necessary to output one type from overload, TS selects the last signature ( |
Thanks so much! |
I was just fiddling around with this question and came up with an answer that 'worked' but really shouldn't have 😂 type UnionToTuple<T> = { [P in T as string]: [P] }[string]; |
If I understand correctly the idea was to first convert union to intersection, and then extract the last element from intersection by the function overloading feature? |
so where's the mentioned in the documentation ? |
|
Thanks to everyone for the explanations! Still, there is one thing that remains unclear:
Where has this been specified? I think it's the first time I see where in TS overloads are getting somehow connected to the rest of the type system. In a very unobvious and unsound way - UPD: It looks like it is indirectly mentioned in the docs here:
Where "type with multiple call signatures" also means functions intersection |
Something changed after TypeScript 2.8 (the doc being mentioned above). The last line isn't |
And to add to what @Alexsey said it does seem that through the method found in this solution you can create a scenario where union/intersection order matters: |
TS didn't change - |
ohhh ok. I was just confused because the docs say |
TS Playground.
The text was updated successfully, but these errors were encountered: