Skip to content

Implementation of concat gives unexpected results #39241

Closed
@DanielRosenwasser

Description

@DanielRosenwasser
function concat1<T extends readonly any[], U extends readonly any[]>(arr1: T, arr2: U): [...T, ...U] {
    return [...arr1, ...arr2];
}

function concat2<T extends readonly any[], U extends readonly any[]>(arr1: T, arr2: U) {
    return [...arr1, ...arr2];
}

const x1 = concat1([1,2,3] as const, [4,5, 6] as const);
const x2 = concat2([1,2,3] as const, [4,5, 6] as const);

Here, TypeScript is okay with concat1 returning [...T, ...U], but instead infers the type of concat2 to be T[number][].

Notice U was not in the output type!

As a result, x1 has the type [1, 2, 3, 4, 5, 6] and x2 has the type (1, 2, 3)[]).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions