Typescript 对象解构导致“预期的属性分配”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34880496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:13:49  来源:igfitidea点击:

Typescript Object destructuring results in "Property assignment expected."

javascripttypescriptecmascript-7destructuring

提问by Rick

I am transitioning a project from Babel to Typescript and receive the following compiler error:

我正在将一个项目从 Babel 转换为 Typescript 并收到以下编译器错误:

error TS1136: Property assignment expected.

from code that looks like this:

从看起来像这样的代码:

var auth = {...this.props.auth};

This code previously worked fine under Babel, but causes the error above when attempting to compile via Typescript. Is object destructuring different in Typescript?

这段代码以前在 Babel 下运行良好,但在尝试通过 Typescript 编译时会导致上述错误。Typescript 中的对象解构不同吗?

回答by MinusFour

The feature you are looking for is Object spread/rest operators(proposed for ES7). It looks like it's planned but not yet implemented:

您正在寻找的功能是Object spread/rest 运算符(建议用于 ES7)。看起来它已计划但尚未实施:

We want to wait for the proposal to reach Stage 3 before addressing this.

在解决这个问题之前,我们希望等待提案达到第 3 阶段。

More info here.

更多信息在这里

Edit: The proposal is in stage-3. We'll likely see it drafted on ES2018 (ES9). Support has been added to TypeScript as well (starting from 2.1).

编辑:该提案处于第 3 阶段。我们很可能会看到它在 ES2018 (ES9) 上起草。也增加了对 TypeScript 的支持(从 2.1 开始)。