Add additional overloads to Array.prototype.concat #26976
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
Search Terms
concat never
Suggestion
Add the following signatures to the
Array
interface: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'.
(withstrictNullChecks
enabled).Examples
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:
The text was updated successfully, but these errors were encountered: