Accessors Are Only Available When Targeting Ecmascript 5 and Higher

I am trying to run this code, but it is giving me the 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";
    }
}
1

21 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.

0

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.

2

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.

My tsconfig.json file

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

And I run it from command line

tsc && node *.js
2

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

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

If you want it inside your tsconfig.json for the entire project configuration:

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

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

ESNext targets the latest supported ES proposed features.

2

In Windows, the

tsc --target es5 filename.ts

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

1

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

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

I had the same issue and got it to work 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

I think that the solution below could be a very helpful command for all developers here.

You can easily solve it using this command in your Terminal, command prompt, Git Bash, etc.:

tsc --target ES2016 Animal.ts --watch

or

tsc --target es5 Animal.ts --watch

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

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

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
  ],

Simply set the "es5" in your tsconfig.json file:

target: "es5"

And that all.

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

There isn't any need to specify the extension:

tsc -target "es5" filename && node filename 

It finally worked out for me. Run the file, and then state the target flag:

tsc script.ts --t es5

Build target not working from tsconfig.json #12645

If using Visual Studio 2017:

  1. Solution Explorer → right-click on the project
  2. Add → New Item
  3. (Search section → type this → typescript)
  4. Select "TypeScript Json Configuration File"

That’s it. At the end of these steps, tsconfig.json will be added to the project with the default set applied to ES5.

You don't have to change anything. Just recompile your project and the error is going to be out.

1

Here you need to pass the switch to the TypeScript compiler, so it targets the ECMAScript file. To do that, enter the below code in the terminal:

tsc (your TypeScript file).ts --target ES5

And run your build JavaScript file:

node (your JavaScript file)

Try using tsc myTS.ts --target ES5.

This is because TypeScript should target the ECMAScript 5 compiler.

I kept having the same issue even after adding this to my tsconfig.json file:

  "compilerOptions": {
    "target": "es5",
    "noEmitOnError": true
  }
}

but that was because I was still running tsc myFileName.ts in the command line. Instead I just needed to run tsc and not include the file name. tsc compiles all the .ts files :).

Just run this command

tsc -target es6 Animal.ts
1

You can generate the tsconfig.json file by the below command.

tsc --init

Use:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest