访问器只有在针对 ECMAScript 5或更高版本时才可用

我正在尝试运行这段代码,但它给了我以下错误:

Ts (10,13) : 错误 TS1056: 访问器只有在以下情况下才可用 针对 ECMAScript 5或更高版本的.Animal.ts (14,13) : 错误 TS1056: 访问器只有在针对 ECMAScript 5或更高版本时才可用。

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";
}
}
92109 次浏览

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.

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.

In Windows, the

tsc --target es5 filename.ts

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

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

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.

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)

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.

Try using tsc myTS.ts --target ES5.

This is because TypeScript should target the ECMAScript 5 compiler.

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 my case, I only needed to add the target.

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

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

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

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

There isn't any need to specify the extension:

tsc -target "es5" filename && node filename

Use:

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

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

target: "es5"

And that all.