-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array concat has wrong types #47351
Comments
As a workaround you can use |
That would create a sparse array, which is deoptimized in engines, so that would never be a good workaround (new Array should never, ever be used). |
|
Duplicate of #36554 (comment). Used search terms: |
Thanks for the workaround suggestion! Unfortunately that doesn’t help me in JS, using JSDoc. |
Your examples work as-is for me in JS, even with const a = /** @type {string[]} */ ([]).concat(
true ? 'a' : [],
false ? 'b' : [],
); But this won't work in a |
@fatcerberus The examples don't work with |
@ljharb you can use |
@SancheZz He's intentionally not using the spread operator. See his first sentence, where he mentions:
|
Also, spread has different semantics - a string is spread into code units, a Map into entries, a Set into values, but the precise semantic I want here is |
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
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
🙂 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.The text was updated successfully, but these errors were encountered: