Renaming remaining properties variable when object destructuring in TypeScript Renaming remaining properties variable when object destructuring in TypeScript typescript typescript

Renaming remaining properties variable when object destructuring in TypeScript


The handbook link you mention covers most of the scenarios you have in your example under destructuring, but I think the one situation it covers is not explicitly covered (only implicitly).

The particular feature you are referring to (I think) is the combination of:

You can create a variable for the remaining items in an object using the syntax ...:

and

You can also give different names to properties:

The handbook doesn't give an example where both are used, as shown below. The interesting part of this combined use is that the ...other token doesn't actually mean anything and is never referenced (or referenceable). In the "working" version of the below example, without the erroring line, the other token doesn't appear in the output at all.

Here is the shortened example:

const values = { a: "1", b: "2", c: "3", d: "4" };const { a: one, ...other: twoThreeFour } = values;console.log(other); // ERROR there is no variable 'other'console.log(a, twoThreeFour); // OK

And the version without the error - the transpiled JavaScript has no reference to other anywhere in the code:

const values = { a: "1", b: "2", c: "3", d: "4" };const { a: one, ...other: twoThreeFour } = values;console.log(a, twoThreeFour); // OK

There is an issue to improve the clarity of the use of rest elements in relation to destructuring in the underlying language specification.