Support concat operations on arrays of different types #26378
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, array, type
Suggestion
Currently concat requires that arrays be of similar types (or arrays of arrays of similar type). This requirement can be too strict at times. Since concat doesn't change the source arrays, it seems reasonable to return an array of the union of the input array types.
This can be achieved by adding an overload to Array.concat:
Use Cases
Joining dissimilar arrays.
Examples
Simple example below. A more realistic example would probably involve arrays of more complex, type-inferred objects that are mostly similar but slightly different.
const nums=[1,2,3];
const strs = ['a','b','c'];
// Currently, must do something like:
const arr1 = (nums as Array<string|number>).concat(strs);
// With this proposal we can instead use:
const arr2 = nums.concat(strs);
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: