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 ECMAS
Here is a simple solution
tsc file.ts --target ES5 && node file.js
Note: Ensure you make use of your file name. This would only be applicable for you if the name of your .ts file is file.
class AnimalImpm {
constructor(private _name?:string){
this.name = name;
}
get name():string{
return this._name;
}
set name(name:string){
this._name = name;
}
sayName():string {
console.log(`My name is ${this.name}`);
return "Hello";
}
}
let data = new AnimalImpm('Animal');
data.name;
data.name = 'newAnimal';
data.sayName();