question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array concat has wrong types

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
MartinJohnscommented, Jan 8, 2022

Duplicate of https://github.com/microsoft/TypeScript/issues/36554#issuecomment-580924501. Used search terms: never array concat

1reaction
MartinJohnscommented, Jan 9, 2022

@SancheZz He’s intentionally not using the spread operator. See his first sentence, where he mentions:

more backwards-compatible

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found