Accessors are only available when targeting ECMAScript 5 and higher

JavascriptTypescriptEcmascript 5Accessor

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

I was having the same problem. What I read from the docs is that the tsconfig.json file is ignored if you specify the files.

enter image description here

My tsconfig.json file

{
  "compilerOptions": {
    "target": "es5"
  },
  "include": [
    "*.ts"
  ]
}

And I run it from command line

tsc && node *.js

Solution 4 - Javascript

In Windows, the

tsc --target es5 filename.ts

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

Solution 5 - Javascript

If you're dealing with single file you could do this

tsc your-file.ts --target ES2016 --watch

If you wants inside your tsconfig.json for entire project configuration

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

You may want to use either ECMAScript numeric "es6", "es7", "es8" or year of release like "ES2015", "ES2016", "ES2017".

ESNext targets latest supported ES proposed features.

Solution 6 - Javascript

I know that is an old conversation, but i think that solution bellow could be a very helpful command for all developers here.

You can easily solve using this command in your Terminal, Command Prompt, Git Bash and etc:

tsc --target ES2016 Animal.ts --watch

or

tsc --target es5 Animal.ts --watch

--watch is optional and means that you doesn't need to compile your code in each modification.

Solution 7 - Javascript

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

 tsc *.ts --target ES5 && node *.js

Solution 8 - Javascript

Had the same issue, got it worked with this... for a single file!

tsc -target "es5" filename && node filename

  • "es5" with quotes

  • need not mention file extensions

  • tsc -target "es5" filename - transpiles the typescript to "es5" JavaScript

  • node filename - runs the transpiled javascript file

Solution 9 - Javascript

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" } ] }

Solution 10 - Javascript

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

Solution 11 - Javascript

I targetted esnext and even with this I got the error. But for me it helped, targeting the .vue files in tsconfig.json. Like:

  "include": [
    "*.ts",
    "*.vue" // << added
  ],

Solution 12 - Javascript

Using Visual Stido 2017 ?
1- Solution Explorer>Right Click To Project
2- Add>New Item
3- (Search section >type this> typescript)
4- Select "TypeScript Json Configuration File"

thats it
at the end of this steps, tsconfig.json will be added to project with default set applied to ES5

you don't have to change anything. just recompile your project and error gonna out.

Solution 13 - Javascript

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)

Solution 14 - Javascript

Try using tsc myTS.ts --target ES5

this is because TypeScript should Traget ECMA script 5 compiler.

Solution 15 - Javascript

Finally worked out for me. Run the file then state the target flag

tsc script.ts --t es5 enter link description here

Solution 16 - Javascript

tsc -target "es5" filename  && node filename 

No need to specify the extension.

Solution 17 - Javascript

tsc .\main.components.ts --target ES5

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjagdish khetreView Question on Stackoverflow
Solution 1 - JavascriptBilalReffasView Answer on Stackoverflow
Solution 2 - JavascriptloctriceView Answer on Stackoverflow
Solution 3 - JavascriptnoWayhomeView Answer on Stackoverflow
Solution 4 - JavascriptVamsi JView Answer on Stackoverflow
Solution 5 - JavascriptAung Zan BawView Answer on Stackoverflow
Solution 6 - JavascriptMarcus CrisostomoView Answer on Stackoverflow
Solution 7 - JavascriptSiddhartha MukherjeeView Answer on Stackoverflow
Solution 8 - JavascriptAditya PatnaikView Answer on Stackoverflow
Solution 9 - JavascriptMassimiliano KrausView Answer on Stackoverflow
Solution 10 - JavascriptJoel OlawanleView Answer on Stackoverflow
Solution 11 - Javascriptgrafzahl-ioView Answer on Stackoverflow
Solution 12 - JavascriptZen Of KursatView Answer on Stackoverflow
Solution 13 - JavascriptMenik TennakoonView Answer on Stackoverflow
Solution 14 - JavascriptRaghavendra DinavahiView Answer on Stackoverflow
Solution 15 - JavascriptPeter MusembiView Answer on Stackoverflow
Solution 16 - JavascriptVarun RameshView Answer on Stackoverflow
Solution 17 - JavascriptMCA DDITView Answer on Stackoverflow