Skip to content

Tuple type vs. array-of-union-type #2189

@rolyp

Description

@rolyp

Have I made a silly mistake here? I thought the tuple type [string, number] was roughly equivalent to the array-of-union-type (string | number)[], and that the following was therefore legal:

function lengths (xs: string[]): [string, number][] {
   return xs.map((x: string) => [x, x.length])
}

However tsc 1.4 complains:

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

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