TypeScript对象解构中剩余属性变量重命名的合法性咨询
Great question—this is a fun edge case that looks like it works at first glance, but actually falls outside of the official spec for both JavaScript and TypeScript.
Let’s start with your example:
const values = { a: "1", b: "2", c: "3", d: "4" }; // Standard destructuring with renaming and rest const { a: va, b: vb, ...other } = values; // The syntax you're curious about const { a: va, b: vb, ...other: vother } = values;
You mentioned this ran in your online test, but couldn’t find documentation supporting it. After digging into the specs and submitting an Issue about this, the conclusion is: this ...other: vother syntax is actually invalid and shouldn’t compile at all.
Here’s why:
- In the ECMAScript (JavaScript) spec, object destructuring’s rest pattern only allows a direct binding identifier—meaning you write
...[variableName], not a renaming syntax like...[existingName]: [newName]. The renaming syntax (property: newName) is reserved for individual named properties, not the rest collection. - TypeScript, as a superset of ES, should enforce this same rule. The fact that it compiled for you was a compiler oversight, not an intentional feature.
And as you noted, this syntax is pretty low-utility anyway—since the other in ...other is already a custom variable name you define, you can just skip the extra renaming step entirely. If you want the rest values in a variable called vother, you can just write:
const { a: va, b: vb, ...vother } = values;
This is fully spec-compliant, intuitive, and achieves the exact same result without any weird edge-case syntax.
内容的提问来源于stack exchange,提问作者Tobías

