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...
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";
}
}
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.
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.
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
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.
In Windows, the
tsc --target es5 filename.ts
options can be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', or 'esnext' (without '' (single quotes)).
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 quotesneed not mention file extensions
tsc -target "es5" filename- transpiles the typescript to "es5" JavaScriptnode 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
If using Visual Studio 2017:
- Solution Explorer → right-click on the project
- Add → New Item
- (Search section → type this → typescript)
- 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.
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
You can generate the tsconfig.json file by the below command.
tsc --init
Use:
tsc .\main.components.ts --target ES5