【问题标题】:Accessor is only available when targeting ECMAScript 5 and higher - error message when using TypeScript访问器仅在针对 ECMAScript 5 及更高版本时可用 - 使用 TypeScript 时出现错误消息
【发布时间】: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


    【解决方案1】:

    试试这个:

    tsc -t es5 script.ts 
    

    当您的编辑器编译目标时,编译器会将目标设置为 ES5。

    【讨论】:

      【解决方案2】:

      问题是在命令行上手动指定源文件时不会考​​虑tsconfig.json

      如果您不带参数运行tsc,那么tsconfig.json 将被自动拾取。

      更多信息请查看issue on GitHub

      【讨论】:

        猜你喜欢
        • 2018-03-14
        • 2017-04-22
        • 1970-01-01
        • 2017-03-14
        • 2019-03-21
        • 2021-07-05
        • 2017-06-29
        • 2017-04-11
        • 2023-02-05
        相关资源
        最近更新 更多