Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional overloads to Array.prototype.concat #26976

Closed
4 tasks done
ajafff opened this issue Sep 8, 2018 · 1 comment
Closed
4 tasks done

Add additional overloads to Array.prototype.concat #26976

ajafff opened this issue Sep 8, 2018 · 1 comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@ajafff
Copy link
Contributor

ajafff commented Sep 8, 2018

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)
@weswigham weswigham added Suggestion An idea for TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Sep 17, 2018
@weswigham weswigham added the In Discussion Not yet reached consensus label Nov 6, 2018
@RyanCavanaugh
Copy link
Member

Please add use cases to #36554

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
3 participants