Skip to content

No type inferrence of callback arguments inside a tuple union type #55632

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

Open
nenikitov opened this issue Sep 5, 2023 · 5 comments · May be fixed by #60434
Open

No type inferrence of callback arguments inside a tuple union type #55632

nenikitov opened this issue Sep 5, 2023 · 5 comments · May be fixed by #60434
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@nenikitov
Copy link

🔎 Search Terms

  • discriminated union inference
  • union argument inference

🕗 Version & Regression Information

  • This is a compilation error in tsserver 3.3.2

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgdgMwgJwILIOZQLwCgpQA+UA2gIwA0UAFAIYBcUcArgLYBGKAlDgHxQA3APYBLACYBdfEVIAmKtXaMAzsGQi4GHtn7DxEgNy4AxkLiqotTI3hI0mHKUo1a2-gG8oAX0O4gA

💻 Code

type InferArg =
  | [1, (a: number) => void]
  | [2, (b: string) => void];
const arg: InferArg = [1, (a) => { }];

🙁 Actual behavior

I get a compilation error:

Parameter 'a' implicitly has an 'any' type.(7006)

🙂 Expected behavior

a should be inferred to be a number because I already passed 1 as the first tuple element. Type inference should be identical to this code:

type InferArg = [1, (a: number) => void];
const arg: InferArg = [1, (a) => { }];

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented 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
Copy link
Contributor

Andarist commented 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 getApparentTypeOfContextualType doesn'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 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.

@nenikitov
Copy link
Author

Thank you for the explanation

@sandersn sandersn added the Bug A bug in TypeScript label Sep 6, 2023
@sandersn sandersn added this to the Backlog milestone Sep 6, 2023
@sandersn sandersn added the Help Wanted You can do this label Sep 6, 2023
fwolff added a commit to fwolff/TypeScript that referenced this issue Oct 28, 2024
@fwolff
Copy link

fwolff commented Oct 28, 2024

I think I have a fix for this issue: fwolff@9eecc30. It works with the code snippet given by @nenikitov, the type of the parameter a is correctly inferred to number.

The change is minimal and inspired by what @Andarist suggested in his comment. Before going further, I'd like to get some feedback from @jcalz (I have been discussing tuple issues with him here and on stackoverflow) or anybody having worked on similar topics.

fwolff added a commit to fwolff/TypeScript that referenced this issue Oct 30, 2024
fwolff added a commit to fwolff/TypeScript that referenced this issue Nov 6, 2024
fwolff added a commit to fwolff/TypeScript that referenced this issue Nov 6, 2024
@fwolff
Copy link

fwolff commented Nov 7, 2024

See my PR: #60434.

fwolff added a commit to fwolff/TypeScript that referenced this issue Dec 2, 2024
fwolff added a commit to fwolff/TypeScript that referenced this issue Dec 3, 2024
fwolff added a commit to fwolff/TypeScript that referenced this issue Dec 3, 2024
fwolff added a commit to fwolff/TypeScript that referenced this issue Dec 3, 2024
fwolff added a commit to fwolff/TypeScript that referenced this issue Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
5 participants