Why this code doesn\'t work?
I have the following app.ts
var a = 1;
var b = 3;
console.log(`Before a = ${a}, b = ${b}`);
[a, b] = [
You are running the code with NodeJS. You aren't transpiling it from TypeScript to JavaScript, so you are trying to treat your TypeScript as if it were JavaScript.
That might be OK, since TypeScript is a superset of JavaScript. In this particular case, your code also happens to be valid JS.
The destructuring assignments you are trying to use are an ES6 feature that are only supported in NodeJS from 6.5 onwards.
You need to upgrade NodeJS to a newer version.
Alternatively, transpile the TypeScript to ES5 with tsc app.ts
You need to compile your code first.
tsc app.ts
Then run it with
node app.js