108

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";
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jagdish khetre
  • 1,381
  • 2
  • 8
  • 15
  • The answer is spread across a couple of different responses here. @jagdish you should answer it yourself! – Raydot Oct 27 '20 at 18:33

21 Answers21

116

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.

BilalReffas
  • 8,132
  • 4
  • 50
  • 71
86

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
loctrice
  • 2,454
  • 1
  • 23
  • 34
  • I cam across the same error, when running the recommended added flag targeting es5 that works for me: tsc script.ts --t es5 However, I would like just to have this configured in my tsconfig.json file. I have pasted my tsconfig.json below. Replacing es2017 by es5 in the target value does not resolve the error for me. By the way targeting es2017 which is higher than es5 anyway and should work as well if I am not mistaken. What am I doing wrong? "compilerOptions": { "lib": ["es2017", "dom"], "module": "commonjs", "target": "es2017", – FriedrichCoen Nov 12 '20 at 10:29
  • It's hard to tell with just seeing the config in a comment. The comment itself is not complete, for example, because there's a trailing comma and no closing }. Start with a very basic test and config and then build the config up with the example. Once that works drop it in your project maybe? – loctrice Nov 13 '20 at 14:06
35

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
noWayhome
  • 670
  • 1
  • 10
  • 16
  • 1
    Wow schizo. I mean I'm old and sort of hate that node/ts/etc. don't really have a standard dialect (ts-node notwithstanding) but then tsc turns around and silently adopts a useless confusing mode when you try to run it over a single file, cute – Britton Kerin Sep 12 '22 at 23:09
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/46722359/edit) (it covers answers and any kind of text as well). Thanks in advance. – Peter Mortensen Oct 26 '22 at 23:42
11

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aung Zan Baw
  • 392
  • 2
  • 8
  • this works for me. set target to "es6", warnings are all gone. – Max Ma Jun 17 '22 at 04:14
  • I tried different supported targets here, and they removed the warnings on the accessors. BUT then any JSX code was not recognized. Only setting "target": "es5" worked for both. – awe Jan 10 '23 at 12:10
8

In Windows, the

tsc --target es5 filename.ts

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vamsi J
  • 601
  • 6
  • 8
  • 3
    Welcome to StackOverflow and thanks for your help. You might want to make your answer even better by adding some explanation. – Kenzo_Gilead Sep 13 '17 at 17:04
4

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

tsc *.ts --target ES5 && node *.js
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Siddhartha Mukherjee
  • 2,703
  • 2
  • 24
  • 29
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aditya Patnaik
  • 1,490
  • 17
  • 27
1

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"
        }
    ]
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
1

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

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

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

target: "es5"

And that all.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gabriel soft
  • 432
  • 3
  • 7
0

If using Visual Studio 2017:

  1. Solution Explorer → right-click on the project
  2. AddNew 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
0

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)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Try using tsc myTS.ts --target ES5.

This is because TypeScript should target the ECMAScript 5 compiler.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

There isn't any need to specify the extension:

tsc -target "es5" filename && node filename 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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 :).

Melissa Heying
  • 1,302
  • 1
  • 4
  • 4
0

Just run this command

tsc -target es6 Animal.ts
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 28 '23 at 13:57
0

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

tsc --init
Debayan
  • 459
  • 4
  • 6
-1

Use:

tsc .\main.components.ts --target ES5
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MCA DDIT
  • 1
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '21 at 08:26