Accessors are only available when targeting ECMAScript 5 and higher

前端 未结 14 1637
北荒
北荒 2020-12-23 13:07

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

14条回答
  •  执念已碎
    2020-12-23 13:59

    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.

    I think a cleaner way of writing that code would have been this

    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();
    

提交回复
热议问题