admin管理员组文章数量:1219997
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] = [b, a];
console.log(`After a = ${a}, b = ${b}`);
when I try to run it with
node app.ts
I have the next:
[a, b] = [b, a];
^
ReferenceError: Invalid left-hand side in assignment
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
But I expect:
Before a = 1, b = 2
After a = 2, b = 1
TypeScript Version 1.8.10
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] = [b, a];
console.log(`After a = ${a}, b = ${b}`);
when I try to run it with
node app.ts
I have the next:
[a, b] = [b, a];
^
ReferenceError: Invalid left-hand side in assignment
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
But I expect:
Before a = 1, b = 2
After a = 2, b = 1
TypeScript Version 1.8.10
Share Improve this question asked Sep 29, 2016 at 16:05 venbervenber 611 gold badge1 silver badge4 bronze badges 1- 5 "TypeScript Version 1.8.10" — You're running it with node. You're running it directly with node. You aren't transpiling it to JavaScript. It is being treated as JavaScript. – Quentin Commented Sep 29, 2016 at 16:07
3 Answers
Reset to default 11You need to compile your code first.
tsc app.ts
Then run it with
node app.js
TypeScript is a transpiled language. tsc, the typescript compiler, transpiles the TypeScript source to JavaScript. Nodejs is a JavaScript environment which wraps the V8 virtual machine.
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
To make it even easier on yourself and run it one command, you can use the ts-node library. After installing to your project, you can just run the following:
ts-node app.ts
本文标签: javascriptTypeScript SyntaxError quotUnexpected tokenquot message in consoleStack Overflow
版权声明:本文标题:javascript - TypeScript: SyntaxError: "Unexpected token" message in console - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739333877a2158609.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论