Skip to content
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

Contextual type for an array element doesn't filter out non-array union members #52588

Closed
Andarist opened this issue Feb 3, 2023 · 1 comment Β· Fixed by #52589
Closed

Contextual type for an array element doesn't filter out non-array union members #52588

Andarist opened this issue Feb 3, 2023 · 1 comment Β· Fixed by #52589
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@Andarist
Copy link
Contributor

Andarist commented Feb 3, 2023

Bug Report

πŸ”Ž Search Terms

array tuple contextual type inference union implicit any

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare function test(
  arg: Record<string, (arg: string) => void> | Array<(arg: number) => void>
): void;

test([
  (arg) => {
    arg; // actual: any; expected: number
  },
]);

πŸ™ Actual behavior

The contextual type for array element is sourcing the information from the string indexer of the Record type. This makes the contextual type of this function to be ((arg: string) => void) | ((arg: number) => void). The contravariant position of arg makes its type reduce to never (since it's string & number) and prevents the expected contextual type to be provided.

πŸ™‚ Expected behavior

I'd expect this contextual type to only be sourced from the Array member

@jakebailey
Copy link
Member

The fix has been reverted; per #53280 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants