问题
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";
}
}
回答1:
The only thing which works for me was to specify the Target
on macOS and Windows.
tsc --target es5 script.ts
You can run the command on your terminal.
回答2:
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.
回答3:
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
回答4:
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.
回答5:
In Windows, the
tsc --target es5 filename.ts
options can be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'esnext'. (without '')
回答6:
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.
回答7:
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.
回答8:
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"
}
]
}
回答9:
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)
回答10:
Try using tsc myTS.ts --target ES5
this is because TypeScript should Traget ECMA script 5 compiler.
回答11:
In my case, I only needed to add the target.
tsc *.ts --target ES5 && node *.js
来源:https://stackoverflow.com/questions/41010780/accessors-are-only-available-when-targeting-ecmascript-5-and-higher