Array concat has wrong types
See original GitHub issueBug Report
A not-uncommon JS idiom is [].concat(x || [], y || [], z || [])
as a faster, simpler, more backwards-compatible [x, y, z].flatMap(x => x || [])
. However, when I try, I get type errors.
🔎 Search Terms
array concat concat overload
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about array concat
⏯ Playground Link
Playground link with relevant code
💻 Code
const a = [].concat(
true ? 'a' : [],
false ? 'b' : [],
);
const x = [].concat(
false ? 'b' : [],
true ? 'a' : [],
);
🙁 Actual behavior
No overload matches this call.
Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Argument of type 'never[] | "a"' is not assignable to parameter of type 'ConcatArray<never>'.
Type 'string' is not assignable to type 'ConcatArray<never>'.
Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Argument of type 'never[] | "a"' is not assignable to parameter of type 'ConcatArray<never>'.
Type 'string' is not assignable to type 'ConcatArray<never>'.(2769)
🙂 Expected behavior
No type errors (except perhaps that the variable may be any
until i provide an explicit type for the resulting array), because this is valid JavaScript since at least ES3.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
typescript concat warning no overloading match - Stack Overflow
I assign to a variable and declare any type I still see the error. javascript · typescript · Share.
Read more >Understanding Array.concat() method in Javascript - hackinbits
In Javascript, we use Array.concat() to concatenate/merge two or more arrays. Array.concat() returns a new array and does not modify the existing arrays....
Read more >How to merge Arrays in TypeScript | bobbyhadz
Use the spread syntax to merge arrays in TypeScript, e.g. `const arr3 ... The final array will have a type that reflects the...
Read more >Concat - GitHub Pages
The key idea here is that it iterates over the elements in array\tuple and pastes them in the place where spread operator is...
Read more >JavaScript Array concat() Method - W3Schools
The array(s) to be concatenated. Return Value. Type, Description. An array, The content from the joined arrays.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Duplicate of https://github.com/microsoft/TypeScript/issues/36554#issuecomment-580924501. Used search terms:
never array concat
@SancheZz He’s intentionally not using the spread operator. See his first sentence, where he mentions: