声明或声明预期javascript /打字稿

Xen*_*Sis 17 javascript typescript

我使用的打字稿1.7和新ES6 systax阵营0.14,我有以下的解构赋值,如解释在这里为好.

let x0, x1, y0, y1;
if(this.props.viewport) {
    {x0, x1, y0, y1} = this.props.viewport;
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到了Declaration or statement expected错误.我究竟做错了什么?

谢谢

Xen*_*Sis 19

所以,我发现了问题.不得不在括号中包裹整行.所以以下是正确的.

let x0, x1, y0, y1;
if(this.props.viewport) {
    ({x0, x1, y0, y1} = this.props.viewport);
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么需要括号? (7认同)
  • PSA:您可能只需要重新启动 Webstorm/VSCode (3认同)
  • @KenRatanachaiS。这是声明后解构的语法,如此处的示例所示 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Syntax (2认同)

Wor*_*orm 7

我遇到了这个问题,因为我试图将其case用作变量名,例如var case = .... 现在我知道这个错误是因为caseJavaScript 的Switch Statement已经使用了。