Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. What am I doing wrong?

Thanks

like image 416
XeniaSis Avatar asked Feb 23 '16 11:02

XeniaSis


People also ask

How do you fix a declaration or statement expected?

If you use IntelliJ as your IDE and are getting the "Declaration or statement expected" error, try closing and re-opening the file or restarting your IDE as it sometimes glitches. Make sure you aren't using any reserved words when declaring variables.

What is expression expected error?

The "Expression expected" TypeScript error occurs when we have a syntax error in our code or our code editor is using an older version of TypeScript. To solve the error, make sure to correct and syntax errors and use a recent version of the TypeScript compiler.


2 Answers

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); } 
like image 194
XeniaSis Avatar answered Oct 09 '22 03:10

XeniaSis


I had this problem because I was trying to use case as a variable name e.g. var case = .... Now I know this error was because case is already used by JavaScript's Switch Statement.

like image 36
Worm Avatar answered Oct 09 '22 04:10

Worm