proposal-destructuring-private icon indicating copy to clipboard operation
proposal-destructuring-private copied to clipboard

Shorthand destructuring syntax

Open bogdanb opened this issue 2 years ago • 2 comments

Did anyone discuss the possibility of using const {#x, #y} = this as a shorthand for const {#x: x, #y: y} = this?

In case someone tries to destructure use both x and #x, this can generate the same error that happens today if you try to do const {x, x} = src. (Typescript complains with “Cannot redeclare block-scoped variable 'x'.”, but in JS at least Chrome seems to allow it and just apply the last assignment.)

bogdanb avatar Jul 07 '22 10:07 bogdanb

That would imply that #x is “# plus an x”, which it is not - the hash is part of the name.

ljharb avatar Jul 07 '22 15:07 ljharb

Yes, the hash is part of the name. But it is also true said name is made of a hash plus an x. The hash is special, since you can’t use just #, or a#b, as a name.

Given that names-starting-with-hash are not legal as variables, that the hash has a special position in names that are legal as properties, and that destructuring already has shorthand rules for creating variables with names taken from properties, it does not feel weird that it would have special shorthand rules to deal with converting names that are valid in one context to names that are valid in another.

In a way, let {x} = this implies that x is at the same time a variable and a property. (Actually, “suggests” is probably more accurate than “implies”.) But what it actually means is that there are two things, one a variable and the other a property, and the former copies the name and the value of the latter. People don’t have too much trouble understanding that distinction, I don’t think the hash thing will be more of a problem.

bogdanb avatar Jul 09 '22 14:07 bogdanb