简体   繁体   English

预期的声明或声明 javascript/typescript

[英]declaration or statement expected javascript/typescript

I am using Typescript 1.7 and React 0.14 with the new ES6 systax and I'm having the following destructuring assignment, as explained here as well.我使用的打字稿1.7和新ES6 systax阵营0.14,我有以下的解构赋值,如解释在这里为好。

let x0, x1, y0, y1;
if(this.props.viewport) {
    {x0, x1, y0, y1} = this.props.viewport;
}

However, I'm getting the Declaration or statement expected error.但是,我收到Declaration or statement expected错误。 What am I doing wrong?我究竟做错了什么?

Thanks谢谢

So, I found the problem.所以,我发现了问题。 Had to wrap the whole line in parenthesis.必须将整行括在括号中。 So the following is correct.所以以下是正确的。

let x0, x1, y0, y1;
if(this.props.viewport) {
    ({x0, x1, y0, y1} = this.props.viewport);
}

I had this problem because I was trying to use case as a variable name eg var case = ... .我有这个问题,因为我试图使用case作为变量名,例如var case = ... Now I know this error was because case is already used by JavaScript's Switch Statement .现在我知道这个错误是因为 JavaScript 的Switch Statement已经使用了case

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

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