Skip to content

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

@Andarist

Description

@Andarist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions