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
{{ message }}
Repository navigation
No type inferrence of callback arguments inside a tuple union type #55632
I think this is one of the issues that prevents #42987 from working. (discriminated unions of tuples don't seem to get contextual typing the way discriminated unions of non-tuple objects do)
This particular case maybe would just work if we'd call discriminateContextualTypeByObjectMembers (arrays are objects, right? 😉 ) but I think that this requires a special function for the array case. Special consideration has to be made for rest elements (maybe some of the logic could be modeled based on getContextualTypeForElementExpression and changes like this one). On top of that, .length should be ignored completely by it when looking for completions since the array literal might still be "incomplete" - so the completions should only care about what has already been typed in and assume that more things might still be typed in.
I think I have a fix for this issue: fwolff@9eecc30. It works with the code snippet given by Mykyta Onipchenko (@nenikitov), the type of the parameter a is correctly inferred to number.
The change is minimal and inspired by what Mateusz Burzyński (@Andarist) suggested in his comment. Before going further, I'd like to get some feedback from Joe Calzaretta (@jcalz) (I have been discussing tuple issues with him here and on stackoverflow) or anybody having worked on similar topics.
Activity
jcalz commentedon Sep 5, 2023
I think this is one of the issues that prevents #42987 from working. (discriminated unions of tuples don't seem to get contextual typing the way discriminated unions of non-tuple objects do)
Andarist commentedon Sep 5, 2023
I had this for a while on my wishlist already - might give it a try soon-ish.
The problem stems from the fact that
getApparentTypeOfContextualTypedoesn't do anything with array literal expressions:https://github.com/microsoft/TypeScript/blob/3f17500cbf0adcdae388a6d161002ab73971f3bf/src/compiler/checker.ts#L29990-L29992
This particular case maybe would just work if we'd call
discriminateContextualTypeByObjectMembers(arrays are objects, right? 😉 ) but I think that this requires a special function for the array case. Special consideration has to be made for rest elements (maybe some of the logic could be modeled based ongetContextualTypeForElementExpressionand changes like this one). On top of that,.lengthshould be ignored completely by it when looking for completions since the array literal might still be "incomplete" - so the completions should only care about what has already been typed in and assume that more things might still be typed in.nenikitov commentedon Sep 5, 2023
Thank you for the explanation
Fix microsoft#55632
fwolff commentedon Oct 28, 2024
I think I have a fix for this issue: fwolff@9eecc30. It works with the code snippet given by Mykyta Onipchenko (@nenikitov), the type of the parameter
ais correctly inferred tonumber.The change is minimal and inspired by what Mateusz Burzyński (@Andarist) suggested in his comment. Before going further, I'd like to get some feedback from Joe Calzaretta (@jcalz) (I have been discussing tuple issues with him here and on stackoverflow) or anybody having worked on similar topics.
Fix microsoft#55632 (taking care of elements / tuples length)
Fix microsoft#55632 (tuple like types)
Fix microsoft#55632 (dprint format)
fwolff commentedon Nov 7, 2024
See my PR: #60434.
Fix microsoft#55632 (don't use isTupleLikeType, use map/filter instea…
Fix microsoft#55632 (better handling of tuple like types)
Fix microsoft#55632 (better handling of tuple like types - hereby bas…
Fix microsoft#55632 (more tests)
Fix microsoft#55632 (mapDefined and TypeFlags.Never handling)