有关代码示例,请在下方滚动。第一部分包含TypeScript
,第二部分包含tsconfig.json
和package.json
。使用tsc(v2.1.6)。
我的问题是如何检测打字稿是否正在提取我的tsconfig.json,请参阅github repo我在哪里解决问题:https://github.com/pluralsight-courses/typescript-fundamentals/tree/master/003-ClassesAndInterface
使用tsc编译命令行时 - 我收到错误src/index.ts(17,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
- 但在tsconfig中我确定它正在针对ES5或以上...我不知道出了什么问题/或者我在这里忽略的。
TypeScript index.ts
class Engine {
constructor(public horsePower: number,
public engineType: string) {
}
}
class Car {
// Fields
private _engine: Engine;
// Constructor(s)
constructor(engine: Engine) {
this.engine = engine;
}
// Properties
get engine(): Engine { return this._engine; }
set engine(value: Engine) {
if (value == undefined) throw 'Supply an Engine!';
this._engine = value;
}
// Functions
start() { return `Started ${this.engine}.`; }
stop() { return `Stopped ${this.engine}.`; }
}
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"sourceMap": true,
"outDir": "app"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
的package.json
{
"name": "classesandinterfaces",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npm run tsc && node app/index.js",
"tsc": "tsc --project ./tsconfig.json",
"tscver": "tsc --version"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "2.1.6"
}
}