Skip to content

Tuple type not inferred when calling generic function #6310

@andy-hanson

Description

@andy-hanson

Tuple types aren't always inferred when calling generic functions. For example:

function identity<A>(value: A): A {
    return value
}

// works
const [a1, b1]: [number, string] = [1, 'a']

// works
const [a2, b2]: [number, string] = identity<[number, string]>([1, 'a'])

// breaks
const [a3, b3]: [number, string] = identity([1, 'a'])

Output:

foo.ts(12,7): error TS2322: Type '(number | string)[]' is not assignable to type '[number, string]'.
  Property '0' is missing in type '(number | string)[]'.

See also #2189, #4136, #6037.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions