访问器仅在针对 ECMAScript 5 及更高版本时可用

问题描述 投票:0回答:22

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

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";
    }
}
javascript typescript ecmascript-5 accessor
22个回答
116
投票

唯一对我有用的是在 macOS 和 Windows 上指定

Target
。请注意参数
-t
代表目标。

tsc -t es5 script.ts

您可以在终端上运行该命令。


88
投票

尝试在项目中设置 tsconfig.json 文件:

{
  "compilerOptions": {
    "target": "es5"
  }
  "files": []
}

这将告诉 TypeScript 编译器专门针对某个版本。


35
投票

我也遇到了同样的问题。我从 docs 中读到的是,如果您指定文件,则 tsconfig.json 文件将被忽略。

enter image description here

我的 tsconfig.json 文件

{
  "compilerOptions": {
    "target": "es5"
  },
  "include": [
    "*.ts"
  ]
}

我从命令行运行它

tsc && node *.js

11
投票

如果您正在处理单个文件,您可以这样做:

tsc your-file.ts --target ES2016 --watch

如果您希望将其放在 tsconfig.json 中以用于整个项目配置:

{ "compilerOptions": { "target": "ES2016" } "files": [] }

您可能需要使用 ECMAScript 数字 "es6"、"es7"、"es8" 或发布年份,例如 "ES2015"、"ES2016"、"ES2017"

ESNext 针对最新支持的 ES 提议功能。


8
投票

在 Windows 中,

tsc --target es5 filename.ts

选项可以是:'es3'、'es5'、'es6'、'es2015'、'es2016'、'es2017'或'esnext'(不带''(单引号))。


4
投票

就我而言,我只需要添加目标。

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

3
投票

我遇到了同样的问题,并让它与此一起工作......对于单个文件!

tsc -target "es5" filename && node filename

  • "es5"
    带引号

  • 无需提及文件扩展名

  • tsc -target "es5" filename
    - 将打字稿转换为“es5”JavaScript

  • node filename
    - 运行 transpiled JavaScript 文件


3
投票

我认为下面的解决方案对于这里的所有开发人员来说可能是一个非常有用的命令。

您可以在终端、命令提示符、Git Bash等中使用此命令轻松解决它:

tsc --target ES2016 Animal.ts --watch

tsc --target es5 Animal.ts --watch

--watch
是可选的,意味着您不需要在每次修改中编译代码。


1
投票

我在尝试使用 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"
        }
    ]
}

1
投票

我的目标是

esnext
,即使这样我也得到了错误。 但对我来说,它有帮助,针对
tsconfig.json
中的 .vue 文件。 喜欢:

  "include": [
    "*.ts",
    "*.vue" // << added
  ],

1
投票

只需在您的 tsconfig.json 文件中设置“es5”即可:

target: "es5"

仅此而已。


1
投票

这是一个简单的解决方案:

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

0
投票

无需指定扩展名:

tsc -target "es5" filename && node filename 

0
投票

它终于对我有用了。运行文件,然后声明目标标志:

tsc script.ts --t es5

构建目标无法从 tsconfig.json #12645


0
投票

如果使用 Visual Studio 2017:

  1. 解决方案资源管理器 → 右键单击项目
  2. 添加新项目
  3. (搜索部分 → 输入此内容 → 打字稿)
  4. 选择“TypeScript Json 配置文件”

就是这样。在这些步骤结束时,tsconfig.json 将添加到项目中,并将默认集应用于 ES5。

您无需更改任何内容。只需重新编译您的项目,错误就会消失。


0
投票

这里需要将开关传递给 TypeScript 编译器,因此它的目标是 ECMAScript 文件。为此,请在终端中输入以下代码:

tsc(您的 TypeScript 文件).ts --目标 ES5

并运行您的构建 JavaScript 文件:

节点(您的 JavaScript 文件)


0
投票

尝试使用

tsc myTS.ts --target ES5

这是因为 TypeScript 应该针对 ECMAScript 5 编译器。


0
投票

即使将其添加到我的 tsconfig.json 文件中,我仍然遇到同样的问题:

  "compilerOptions": {
    "target": "es5",
    "noEmitOnError": true
  }
}

但那是因为我仍在命令行中运行

tsc myFileName.ts
。相反,我只需要运行
tsc
并且不包含文件名。
tsc
编译所有
.ts
文件:)。


0
投票

只需运行此命令

tsc -target es6 Animal.ts

0
投票

您可以通过以下命令生成tsconfig.json文件。

tsc --init

0
投票

这对我有用 -

{ “编译器选项”:{ “目标”:“ES2018” }

}


-1
投票

用途:

tsc .\main.components.ts --target ES5
© www.soinside.com 2019 - 2024. All rights reserved.