【问题标题】:Accessors are only available when targeting ECMAScript 5 and higher访问器仅在面向 ECMAScript 5 及更高版本时可用
【发布时间】:2017-04-22 00:03:06
【问题描述】:

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

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";
    }
}

【问题讨论】:

  • 答案在这里分布在几个不同的响应中。 @jagdish 你应该自己回答!

标签: javascript typescript ecmascript-5 accessor


【解决方案1】:

尝试在您的项目中设置一个 tsconfig.json 文件:

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

这将告诉 typescript 编译器专门针对一个版本。

【讨论】:

  • 在运行针对我的推荐添加的针对 es5 的标志时,我遇到了同样的错误:tsc script.ts --t es5 但是,我只想在我的 tsconfig 中配置它。 json 文件。我在下面粘贴了我的 tsconfig.json。在目标值中用 es5 替换 es2017 并不能解决我的错误。顺便说一句,目标 es2017 无论如何都高于 es5,如果我没记错的话应该也能正常工作。我究竟做错了什么? "compilerOptions": { "lib": ["es2017", "dom"], "module": "commonjs", "target": "es2017",
  • 仅在评论中看到配置很难判断。注释本身并不完整,例如,因为结尾有逗号且没有结束 }。从一个非常基本的测试和配置开始,然后使用示例构建配置。一旦可行,可能会将其放入您的项目中?
【解决方案2】:

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

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案3】:
tsc -target "es5" filename  && node filename 

无需指定扩展名。

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      遇到了同样的问题,解决了这个问题...对于单个文件!

      tsc -target "es5" filename &amp;&amp; node filename

      • "es5" 带引号
      • 不必提及文件扩展名

      • tsc -target "es5" filename - 将 typescript 转换为 "es5" JavaScript

      • node filename - 运行转译后的 javascript 文件

      【讨论】:

        【解决方案6】:

        终于为我解决了。运行文件然后声明目标标志

        tsc script.ts --t es5 enter link description here

        【讨论】:

          【解决方案7】:

          这是一个简单的解决方案

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

          【讨论】:

            【解决方案8】:

            唯一对我有用的是在 macOS 和 Windows 上指定 Target。请注意参数-t代表Target。

            tsc -t es5 script.ts
            

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

            【讨论】:

            • 但是实际上不要用单引号包围 ES5,否则你会得到像 only 'es5', 'es6', etc... are valid arguments for target 这样的令人困惑的错误等等,这正是我输入的,'es5'。但是,应该是tsc --target es5 script.ts
            • 以上命令出错。需要从“ES5”中删除引号才能运行它
            • tsc -t es5 script.ts- 快捷方式
            【解决方案9】:

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

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

            【讨论】:

              【解决方案10】:

              如果您处理的是单个文件,您可以这样做

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

              如果您想在 tsconfig.json 中进行整个项目配置

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

              您可能希望使用 ECMAScript 数字 "es6"、"es7"、"es8" 或发布年份,如 "ES2015"、"ES2016"、"ES2017" em>。

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

              【讨论】:

                【解决方案11】:

                尝试使用 tsc myTS.ts --target ES5

                这是因为 TypeScript 应该 Traget ECMA script 5 编译器。

                【讨论】:

                  【解决方案12】:

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

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

                  tsc --target ES2016 Animal.ts --watch

                  tsc --target es5 Animal.ts --watch

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

                  【讨论】:

                    【解决方案13】:

                    这里您需要将开关传递给类型脚本编译器,因此它以 ECMAScript 文件为目标。为此,请在终端中输入以下代码

                    tsc (你的 ts 文件).ts --target ES5 并运行你的 bulid js 文件

                    节点(你的 js 文件)

                    【讨论】:

                      【解决方案14】:

                      使用 Visual Stido 2017 吗?
                      1- 解决方案资源管理器>右键单击项目
                      2- 添加>新项目
                      3-(搜索部分>输入此>打字稿)
                      4- 选择“TypeScript Json 配置文件”

                      就是这样
                      在此步骤结束时,tsconfig.json 将被添加到项目中,默认设置应用于 ES5

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

                      【讨论】:

                      • typescript 在搜索框中没有给出任何结果。有什么想法吗?
                      【解决方案15】:

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

                      我的 tsconfig.json 文件

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

                      我从命令行运行它

                      tsc && node *.js
                      

                      【讨论】:

                        【解决方案16】:

                        我在尝试使用 Visual Studio Code 编译 TypeScript 代码时遇到了同样的问题。 这解决了这个问题:

                        1) tsconfig.json - 在compilerOptions 中添加target

                        {
                            "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"
                                }
                            ]
                        }
                        

                        【讨论】:

                          【解决方案17】:

                          在 Windows 中,

                          tsc --target es5 filename.ts
                          

                          选项可以是:“es3”、“es5”、“es6”、“es2015”、“es2016”、“es2017”、“esnext”。 (不带'')

                          【讨论】:

                          • 欢迎来到 StackOverflow 并感谢您的帮助。您可能希望通过添加一些解释来使您的答案变得更好。
                          猜你喜欢
                          • 2018-03-14
                          • 1970-01-01
                          • 2021-05-23
                          • 2021-07-05
                          • 2017-04-11
                          • 2017-03-14
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多