Accessors are only available when targeting ECMAScript 5 and higher

前端 未结 14 1630
北荒
北荒 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:53

    Try using tsc myTS.ts --target ES5

    this is because TypeScript should Traget ECMA script 5 compiler.

    0 讨论(0)
  • 2020-12-23 13:57

    In Windows, the

    tsc --target es5 filename.ts
    

    options can be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'esnext'. (without '')

    0 讨论(0)
  • 2020-12-23 13:57

    I had the same problem trying to compile TypeScript code with Visual Studio Code. This solved the issue:

    1) tsconfig.json - add the target in the compilerOptions:

    {
        "compilerOptions": {
            "target": "es5",  // <-- here!
            "module": "commonjs",
            "sourceMap": true
        }
    }
    

    2) tasks.json - add the target argument:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "taskName": "build",
                "type": "process",
                "command": "tsc",
                "args": [
                    "ts/Program.ts",
                    "--outDir", "js",
                    "--sourceMap",
                    "--watch",
                    "--target", "es5"  // <-- here!
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "presentation": {
                    "reveal": "always"
                },
                "problemMatcher": "$tsc"
            }
        ]
    }
    
    0 讨论(0)
  • 2020-12-23 13:57

    Here you need to pass the switch to type script compiler, so it target ECMAScript file. To do that enter below code in the terminal

    tsc (your ts file).ts --target ES5 and run your bulid js file

    node (your js file)

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-23 14:03

    In my case, I only needed to add the target.

     tsc *.ts --target ES5 && node *.js
    
    0 讨论(0)
提交回复
热议问题