简体   繁体   English

在TypeScript中进行对象解构时重命名剩余的属性变量

[英]Renaming remaining properties variable when object destructuring in TypeScript

EDIT: 编辑:

I opened an issue related to this on github: https://github.com/Microsoft/TypeScript/issues/21265 我在github上打开了与此相关的问题: https//github.com/Microsoft/TypeScript/issues/21265
It seems that { ...other: xother } is not valid JS, neither TS, code and it should not even compile. 似乎{ ...other: xother }是无效的JS,TS,代码都不应该编译。

Original question: 原始问题:

Let's assume the following example: 我们假设以下示例:

const values = { a: "1", b: "2", c: "3", d: "4" };
const { a: va, b: vb, ...other } = values;

where a new variable name va is assigned for property a . 其中为属性a分配了新的变量名称va

Is it valid, according to TypeScript specs, to do the same with the remaining properties ...other ? 根据TypeScript规范,对其余属性执行相同的操作是否有效...other Something like: 就像是:

const { a: va, b: vb, ...other: vother } = values;

I know it works, I've tested it ( online test ). 我知道它有效,我已经测试过了( 在线测试 )。 But I'm unable to find where it's defined to do so. 但我无法找到它定义的位置。

The examples of the documents that refers to property renaming always show the "normal" case: TypeScript Handbook - Variable Declarations . 引用属性重命名的文档示例始终显示“正常”情况: TypeScript Handbook - 变量声明 And the spec grammar refres to a BindingPattern that's no described (or I've missed) in the spec: TypeScript Spec . 并且规范语法在规范: TypeScript Spec中没有描述(或者我已经错过)的BindingPattern

My first impression is that it is not very useful since ...other is already a custom variable name. 我的第一印象是它不是很有用,因为...other已经是自定义变量名称。 But it works. 但它的确有效。 And I'm curious to know where it is specifically defined or if it's only a corner case that works (I suppose not). 而且我很想知道它的具体定义在哪里,或者它只是一个有效的角落(我想不是)。

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). 这种组合使用的有趣部分是...other标记实际上并不意味着什么,并且从未被引用(或可引用)。 In the "working" version of the below example, without the erroring line, the other token doesn't appear in the output at all. 在下面示例的“工作”版本中,没有错误行, other令牌根本不会出现在输出中。

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: 没有错误的版本 - 转换后的JavaScript没有引用代码中的other任何地方:

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. 在基础语言规范中,有一个问题是提高与重构相关的rest元素的使用的清晰度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM