Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessors are only available when targeting ECMAScript 5 and higher

I am trying to run this code but it is giving me following errors:

Animal.ts(10,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. Animal.ts(14,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.

interface IAnimal{     name : string;     sayName():string; }  class AnimalImpm implements IAnimal{     private _name : string = '[Animal]';     get name():string{         return this._name;     }      set name(name:string){         this._name = name;     }      constructor(name:string){         this.name = name;     }      sayName():string {         console.log(`My name is ${this.name}`);         return "Hello";     } } 
like image 708
jagdish khetre Avatar asked Dec 07 '16 06:12

jagdish khetre


People also ask

How do you fix accessors are only available when targeting ECMAScript 5 and higher?

To solve the error "Accessors are only available when targeting ECMAScript 5 and higher", set the target property to es6 in your tsconfig. json file or use the --target es6 flag when running the tsc command.

What is es5 and es2015?

es5 is the 5th Edition ; es6 / es2015 is the 6th Edition of ECMAScript ; es7 / es2016 is the 7th Edition of ECMAScript ; So I am confused as to why I see es5 and es2015 as part of the bundles.

What is the default ECMAScript target version given by parameter?

Specify ECMAScript target version. Default value is ES3 .

What is the latest version of ECMAScript?

11th Edition – ECMAScript 2020.


2 Answers

The only thing which worked for me was to specify the Target on macOS and Windows. Please note the parameter -t is standing for Target.

tsc -t es5 script.ts 

You can run the command on your terminal.

like image 145
BilalReffas Avatar answered Sep 23 '22 13:09

BilalReffas


try setting up a tsconfig.json file in your project:

{   "compilerOptions": {     "target": "es5"   }   "files": [] } 

that will tell the typescript compiler to target a version specifically.

like image 30
loctrice Avatar answered Sep 19 '22 13:09

loctrice