2016-09-29 3 views
1

이 코드가 작동하지 않는 이유는 무엇입니까?TypeScript : SyntaxError : 콘솔에서 "예기치 않은 토큰"메시지가 나타납니다.

[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 

하지만 기대 :

나는 다음과 같은 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}`); 

내가

node app.ts

그것을 실행하려고 나는 다음을 가지고있다

Before a = 1, b = 2 
After a = 2, b = 1 

TypeScript 버전 1.8.10

+4

"타이프 라이터 버전 1.8.10"로 ES5합니다 - 당신은 노드를 실행 중입니다. 노드로 직접 실행하고 있습니다. 자바 스크립트로 변환하지 않습니다. 그것은 JavaScript로 취급되고 있습니다. – Quentin

답변

2

코드를 먼저 컴파일해야합니다.

tsc app.ts 

그런

node app.js 
2

당신은 NodeJS와 코드를 실행하여 실행합니다. TypeScript에서 JavaScript로 변환하지 않으므로 TypeScript를 JavaScript 인 것처럼 취급하려고합니다.

TypeScript는 JavaScript의 상위 집합이므로 괜찮을 수도 있습니다. 이 특별한 경우에는 코드가 유효한 JS이기도합니다.

사용하려는 destructuring assignments은 6.5 이후의 NodeJS에서만 지원되는 ES6 기능입니다.

NodeJS를 최신 버전으로 업그레이드해야합니다.

또는, 타이프 라이터 transpile tsc app.ts