Skip to content

Add additional overloads to Array.prototype.concat #26976

Closed
@ajafff

Description

@ajafff

Search Terms

concat never

Suggestion

Add the following signatures to the Array interface:

interface Array<T> {
    concat<U>(this: Array<never>, ...items: ConcatArray<U>[]): U[];
    concat<U>(this: Array<never>, ...items: (U | ConcatArray<U>)[]): U[];
}

For my use case this is not needed in ReadonlyArray, but could be added for consistency.

Use Cases

I often find myself in the need of using Array.prototype.flatMap, but since I target Node.js 6 it's not available. Therefore I try to replace it with [].concat(someArray.map(mightReturnArray)).
But then I get an error like Type 'whatever' is not assignable to type 'never'. (with strictNullChecks enabled).

Examples

interface Array<T> {
    /**
     * Combines two or more arrays.
     * @param items Additional items to add to the end of array1.
     */
    concat<U>(this: Array<never>, ...items: ConcatArray<U>[]): U[];
    /**
     * Combines two or more arrays.
     * @param items Additional items to add to the end of array1.
     */
    concat<U>(this: Array<never>, ...items: (U | ConcatArray<U>)[]): U[];
}

// now OK
[].concat(['a']);
['a'].concat(['b']);

// still an error as expected
[1].concat(['a']);

http://www.typescriptlang.org/play/#src=interface%20Array%3CT%3E%20%7B%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20Combines%20two%20or%20more%20arrays.%0A%20%20%20%20%20*%20%40param%20items%20Additional%20items%20to%20add%20to%20the%20end%20of%20array1.%0A%20%20%20%20%20*%2F%0A%20%20%20%20concat%3CU%3E(this%3A%20Array%3Cnever%3E%2C%20...items%3A%20ConcatArray%3CU%3E%5B%5D)%3A%20U%5B%5D%3B%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20Combines%20two%20or%20more%20arrays.%0A%20%20%20%20%20*%20%40param%20items%20Additional%20items%20to%20add%20to%20the%20end%20of%20array1.%0A%20%20%20%20%20*%2F%0A%20%20%20%20concat%3CU%3E(this%3A%20Array%3Cnever%3E%2C%20...items%3A%20(U%20%7C%20ConcatArray%3CU%3E)%5B%5D)%3A%20U%5B%5D%3B%0A%7D%0A%0A%2F%2F%20now%20OK%0A%5B%5D.concat(%5B'a'%5D)%3B%0A%5B'a'%5D.concat(%5B'b'%5D)%3B%0A%0A%2F%2F%20still%20an%20error%20as%20expected%0A%5B1%5D.concat(%5B'a'%5D)%3B
Remember to enable strictNullChecks

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptIn 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