在TypeScript中解构对象时重命名其余属性变量 [英] Renaming remaining properties variable when object destructuring in TypeScript

查看:245
本文介绍了在TypeScript中解构对象时重命名其余属性变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在github上打开了一个与此相关的问题: https://github.com/Microsoft/TypeScript/issues/21265
似乎 {... other:xother} 不是有效的JS,TS,代码,甚至都不应该编译.

I opened an issue related to this on 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.

原始问题:

让我们假设以下示例:

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

其中为属性 a 分配了新的变量名称 va .

where a new variable name va is assigned for property a.

根据TypeScript规范,对其余属性 ... other 执行相同操作是否有效?像这样:

Is it valid, according to TypeScript specs, to do the same with the remaining properties ...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. And the spec grammar refres to a BindingPattern that's no described (or I've missed) in the spec: TypeScript Spec.

我的第一印象是它不是很有用,因为 ... other 已经是一个自定义变量名称.但这行得通.而且我很想知道它的具体定义是什么,或者它只是一个极端的案例(我想不是).

My first impression is that it is not very useful since ...other is already a custom variable name. 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 ...:

您还可以为属性指定不同的名称:

You can also give different names to properties:

该手册没有给出两个都使用的示例,如下所示.这种组合使用的有趣之处在于, ... other 令牌实际上并不意味着任何东西,并且永远不会被引用(或可引用).在下面示例的工作"版本中,没有错误行, other 标记根本没有出现在输出中.

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.

以下是简化的示例:

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

版本没有错误-编译的JavaScript在代码中的任何地方都没有引用 other :

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

有一个问题可以提高使用其他元素进行销毁方面的清晰度.

这篇关于在TypeScript中解构对象时重命名其余属性变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆