【发布时间】:2021-05-23 05:51:19
【问题描述】:
当我尝试使用“tsc file.ts”编译以下 TypeScript 时,我收到以下错误(两次):
错误 TS1056:访问器仅在面向 ECMAScript 5 及更高版本时可用。
根据 StackOverflow 上的这篇文章 - Accessors are only available when targeting ECMAScript 5 and higher - 我应该能够指定一个“tsconfig.json”文件,我已经完成了:
{
"compilerOptions": {
"target": "ES5"
}
}
export class CPoint {
constructor(private _x?: number, private _y?: number) {
};
public draw() {
console.log(`x: ${this._x} y: ${this._y}`);
}
public get x() {
return this._x;
}
public set x(value: number) {
if (value < 0) {
throw new Error('The value cannot be less than 0.');
}
this._x = value;
}
}
我可以使用 --target "ES5" 进行编译,但为什么 tsc 不读取我的 .json 文件? 我不想在每次编译时都指定 ES5。
【问题讨论】:
标签: typescript