简体   繁体   English

ECMAScript 6 在对象解构中传播语法。 支持 TypeScript 和 Babel

[英]ECMAScript 6 spread syntax in object deconstruction. Support in TypeScript and Babel

Is the following valid ECMAScript 6?以下是有效的 ECMAScript 6 吗? It seems to be supported by the latest version of Babel but it isn't by TypeScript.最新版本的 Babel 似乎支持它,但 TypeScript 不支持它。 I couldn't find any ES6 references dealing with this case.我找不到任何处理这种情况的 ES6 参考。

var a = { foo : 'foo' };
var b = { ...a };

No, this is not valid ECMAScript 6. ES6 does only support rest syntax in function parameters and array destructuring, and spread syntax in function calls and array construction.不,这不是有效的 ECMAScript 6。ES6 仅支持函数参数和数组解构中的 rest 语法,以及函数调用和数组构造中的传播语法。

It seems to be supported by the latest version of Babel好像是最新版的 Babel 支持的

Babel does implement the objectRestSpread ES7 proposal as a experimental plugin . Babel 确实将objectRestSpread ES7 提案实现为一个实验性插件 You shouldn't use this feature, it may break at any time.您不应使用此功能,它可能随时中断。

TypeScript 2.1 does support this feature. TypeScript 2.1 确实支持此功能。

Here 这里

I was making following mistake我犯了以下错误

const o = { p : { q:1, r:2 } };
const {{q,r}} = o;

later realized that it is important for me to direct q and r from p , so it was basically a syntax error in my case, so corrected code with following syntax.后来意识到从 p引导qr对我很重要,所以在我的情况下它基本上是一个语法错误,所以用以下语法更正了代码。

const {p:{q,r,s=9}} = o;
console.log(q,r,s); // 1,2,9

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

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