我正在尝试运行此代码,但它给了我以下错误:
Animal.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";
}
}
唯一对我有用的是在 macOS 和 Windows 上指定
Target
。请注意参数-t
代表目标。
tsc -t es5 script.ts
您可以在终端上运行该命令。
尝试在项目中设置 tsconfig.json 文件:
{
"compilerOptions": {
"target": "es5"
}
"files": []
}
这将告诉 TypeScript 编译器专门针对某个版本。
我也遇到了同样的问题。我从 docs 中读到的是,如果您指定文件,则 tsconfig.json 文件将被忽略。
我的 tsconfig.json 文件
{
"compilerOptions": {
"target": "es5"
},
"include": [
"*.ts"
]
}
我从命令行运行它
tsc && node *.js
如果您正在处理单个文件,您可以这样做:
tsc your-file.ts --target ES2016 --watch
如果您希望将其放在 tsconfig.json 中以用于整个项目配置:
{ "compilerOptions": { "target": "ES2016" } "files": [] }
您可能需要使用 ECMAScript 数字 "es6"、"es7"、"es8" 或发布年份,例如 "ES2015"、"ES2016"、"ES2017"。
ESNext 针对最新支持的 ES 提议功能。
在 Windows 中,
tsc --target es5 filename.ts
选项可以是:'es3'、'es5'、'es6'、'es2015'、'es2016'、'es2017'或'esnext'(不带''(单引号))。
就我而言,我只需要添加目标。
tsc *.ts --target ES5 && node *.js
我遇到了同样的问题,并让它与此一起工作......对于单个文件!
tsc -target "es5" filename && node filename
"es5"
带引号
无需提及文件扩展名
tsc -target "es5" filename
- 将打字稿转换为“es5”JavaScript
node filename
- 运行 transpiled JavaScript 文件
我在尝试使用 Visual Studio Code 编译 TypeScript 代码时遇到了同样的问题。 这解决了问题:
1) tsconfig.json - 在
target
:中添加
compilerOptions
{
"compilerOptions": {
"target": "es5", // <-- here!
"module": "commonjs",
"sourceMap": true
}
}
2) tasks.json - 添加
target
参数:
{
"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"
}
]
}
我的目标是
esnext
,即使这样我也得到了错误。
但对我来说,它有帮助,针对 tsconfig.json
中的 .vue 文件。
喜欢:
"include": [
"*.ts",
"*.vue" // << added
],
只需在您的 tsconfig.json 文件中设置“es5”即可:
target: "es5"
仅此而已。
这是一个简单的解决方案:
tsc file.ts --target ES5 && node file.js
注意: 确保使用您的文件名。仅当您的 .ts 文件名称为 file.
时,这才适用于您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();
无需指定扩展名:
tsc -target "es5" filename && node filename
如果使用 Visual Studio 2017:
就是这样。在这些步骤结束时,tsconfig.json 将添加到项目中,并将默认集应用于 ES5。
您无需更改任何内容。只需重新编译您的项目,错误就会消失。
这里需要将开关传递给 TypeScript 编译器,因此它的目标是 ECMAScript 文件。为此,请在终端中输入以下代码:
tsc(您的 TypeScript 文件).ts --目标 ES5
并运行您的构建 JavaScript 文件:
节点(您的 JavaScript 文件)
尝试使用
tsc myTS.ts --target ES5
。
这是因为 TypeScript 应该针对 ECMAScript 5 编译器。
即使将其添加到我的 tsconfig.json 文件中,我仍然遇到同样的问题:
"compilerOptions": {
"target": "es5",
"noEmitOnError": true
}
}
但那是因为我仍在命令行中运行
tsc myFileName.ts
。相反,我只需要运行 tsc
并且不包含文件名。 tsc
编译所有 .ts
文件:)。
只需运行此命令
tsc -target es6 Animal.ts
您可以通过以下命令生成tsconfig.json文件。
tsc --init
这对我有用 -
{ “编译器选项”:{ “目标”:“ES2018” }
}
用途:
tsc .\main.components.ts --target ES5