Skip to content

map on tuples with more than 5 elements #13840

@John-Colvin

Description

@John-Colvin

TypeScript Version: 2.1.1

Code

const a: [number, number, number, number, number, number] = [1,2,3,4,5,6];
const b = a.map(x => x)

Expected behavior:
b should have the same type as a, i.e. a tuple of 6 numbers
Actual behavior:
b has the type of a tuple of 5 numbers. Not only is this unexpected, it also doesn't match what happens at runtime where b.length == a.length as expected.

It seems to me that the problem comes from the tuple overloads of map added in #11252.
If I understand this correctly, #5453 would be needed in order to these overloads correctly for the general case.
In the meantime if there was some way of falling back to an array for map on longer tuples, that would work, e.g. adding an overload like this maybe?

map<Q extends [T, T, T, T, T, T], U>(this: Q, callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions