Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upEmpty object destructuring could tolerate null and undefined #1205
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
claudepache
May 24, 2018
Contributor
For me, {/* whatever */} as function parameter suggests an object to be destructured, regardless whether /* whatever */ is empty or not (and the emptiness of /* whatever */ might change in the course of refactoring); so that the TypeError in third(null, undefined, 5) is justified.
Instead of some clever hack, why not just use something boring like:
function third(_1, _2, x) { return x }
function third(dummy, dummy2, x) { return x }?
|
For me, Instead of some clever hack, why not just use something boring like: function third(_1, _2, x) { return x }
function third(dummy, dummy2, x) { return x }? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
pygy
May 24, 2018
Destructuring in function signatures is about binding, and function ({}, x) { return x } is in general a great, native way to say "Please don't bind any name to the first argument", not a clever hack.
FWIW, it already tolerates non-objects like strings, numbers and booleans.
Edit: we could alternatively devise a new third ( , , x) {return x} syntax, but I'm not a fan of those lone comas even though there is a precedent with [ , , ]. It would also make weird TypeScript/Flow type signature if you wanted your function to ignore the params, yet be picky type-wise about what it ignores.
pygy
commented
May 24, 2018
•
|
Destructuring in function signatures is about binding, and FWIW, it already tolerates non-objects like strings, numbers and booleans. Edit: we could alternatively devise a new |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
barneycarroll
May 24, 2018
I support this proposal. I think explicitly destructuring to an empty object literal is clear enough in its intent to produce no reference: if the destructuring expression contained a property reference, then it's clear there would be a problem with interpolating undefined. As it is, the stated reason for the failure to destructure undefined values to {} don't match the stated rationale: it's perfectly possible to do this for non-nullish values, and since there's no attempted property interpolation it's not clear why nullish values throw.
barneycarroll
commented
May 24, 2018
|
I support this proposal. I think explicitly destructuring to an empty object literal is clear enough in its intent to produce no reference: if the destructuring expression contained a property reference, then it's clear there would be a problem with interpolating |
pygy commentedMay 24, 2018
•
edited
In strict mode, this function declaration is a SyntaxError:
A nice alternative is
But then
third(null, undefined, 5)throws aTypeError. It would be great if it didn't.The change is AFAICT Web compatible since this is currently an error. It also seems logical to tolerate nulls here since the empty destructuring doesn't try to get anything from its source value.
This may be at odds with #1163 (or at least it would be useful to think both issues holistically).
Edit: cc @barneycarroll whose tweet triggered this message