I'm trying to learn TypeScript and Angular, but there's a strange error that keeps bugging me.
In short, I import a class named Point from the module point. The error suggests that a curly brace is wrong. The error points to the import statement in main.ts (line number 1).
Furthermore, I have the .ts and .js files in a folder. This folder does not have a tsconfig.json file.
I transpile and run the code with:
tsc main.ts --target ES2016 && node main.js
I target ES2016 for the support of getter and setter properties.
error
main.js:1
(function (exports, require, module, __filename, __dirname) { import { Point } from './point';
SyntaxError: Unexpected token {
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
main.ts
import { Point } from './point';
let point = new Point(1);
point.draw();
let x = point.x;
console.log(x);
point.y = 500;
point.draw();
point.ts
export class Point {
constructor(private _x?: number, private _y?: number) { }
get x() {
return this._x;
}
set y(value) {
if (value < 0)
throw new Error('New X must be 0 or higher.');
this._y = value
}
draw() {
console.log('X: ' + this._x + ', Y: ' + this._y);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.