-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
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']);
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)
j-oliveras, sean-vieira, zenmumbler, andersk, NN--- and 4 more
Metadata
Metadata
Assignees
Labels
Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript