1

When I try to compile the below TypeScript using "tsc file.ts," I get the following error (twice):

error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.

According to this article on StackOverflow - Accessors are only available when targeting ECMAScript 5 and higher -- I should be able to specify a "tsconfig.json" file, which I've done:

{
  "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;
  }
}

I can compile using --target "ES5" but why doesn't tsc read my .json file? I don't want to have to specify ES5 on every compile.

Gary
  • 909
  • 9
  • 26

2 Answers2

10

The issue is that tsconfig.json isn't considered when you manually specify your source files on the command line.

If you run tsc with no arguments then the tsconfig.json will be picked up automatically.

For more information check this issue on GitHub

Altrim
  • 6,536
  • 4
  • 33
  • 36
1

Try this:

tsc -t es5 script.ts 

The compiler sets the target to ES5 when your editor compiles it.

enzo
  • 9,861
  • 3
  • 15
  • 38