-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 spread throws on undefined/null but object spread doesn't #687
Comments
The right place would be the repo for the object spread proposal - the rationale is because it 1:1 maps to |
Yeah, I prefer that behaviour actually. I was more hoping that array spread could adopt the same behaviour but I suppose that ship has sailed? |
Array spread isn't "array spread", it's "iterable spread", so different rules apply. |
Would a proposal to modify "iterable spread" to ignore undefined/null be considered? |
That would be a breaking change for anybody who was relying on the throw behavior. What would be the benefit of that, when you can do |
Yes, it would indeed be breaking which is why I supposed the ship had sailed. Does Ecmascript ever break forward compatibility? |
This little "gotcha" just caught me by surprise too. I can understand the history of how this happened, but it does look like a mistake in language design from my perspective. Maintaining equivalency between the two spread operator syntaxes helps developers. Javascript already has too many gotchas. I'd hope we could avoid introducing more. |
I was scratching my head trying to find a bug in an application, when I realized that it was due to something like var obj = { ...otherObj.someUndefinedValue } I strongly believe that errors would be super helpful here. As with arrays, it would also easy to write var obj = { ...(otherObj.someUndefinedValue || {}) } to explicitly opt out of error behavior. But it would be too late to change the spec, right? |
Yes, it would break a ton of code if that started throwing. |
You have to write |
Edit: nm, didn’t catch the ‘singular’ |
@Lukom no reason to then spread that into another array, keep it const x = [].concat(maybeArrOrObject || []); hell you can throw as many args into concat as you want so even if you need additional items added there's no reason for the outer spread. on the topic at hand, I agree with the poster up a ways, that it is surprising to me that this sort of confusing "gotcha" made it into the language during a time when there's been such an effort to make JS less confusing for newcomers and novices. I certainly can appreciate why it ended up this way, but for syntax that is aesthetically identical, it is a bit surprising that it behaves differently for the 2 "main" collection types used in the lang (there are many more now but you know what I mean). (also strange I'd never considered specifically this, until just now). |
People are being mislead by |
You can use |
Yay JavaScript! for {...null} working but not (...null). (tc39/ecma262#687)
The number of likes in this thread shows that there is clearly a language issue. |
Lots of code relies on it, it’s not safe to fix, and i doubt anyone would have the appetite to risk it. |
(Pardon if this isn't the right place to discuss this)
I've noticed an annoying inconsistency between how object and array spread behave, namely that object spread silently ignores
null
/undefined
while array spread throws an error:I was wondering what was the rationale for array spread throwing an error on
undefined
/null
and if perhaps object spread could adopt the same behaviour or vice versa.The text was updated successfully, but these errors were encountered: