Skip to content

concat arrays #41913

@SancheZz

Description

@SancheZz

TypeScript Version: 4.1.2 & Nightly 4.2

Search Terms: concat, spread

Code

type First = [number, string];
type Second = [string, string];
type Result = [...First, ...Second];

function concat (first: First, second: Second): Result {
    return first.concat(second); // error
}

function spread (first: First, second: Second): Result {
    return [...first, ...second]; // ok
}

Expected behavior: concat function works similarly to spread function without an error.

Actual behavior: Type '(string | number)[]' is not assignable to type '[number, string, string, string]'. Target requires 4 element(s) but source may have fewer.(2322)

Playground Link:
playground

Related Issues: #36554

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions