【问题标题】:Typescript compiler missing ECMAScript 5 support when I am targeting ES6当我以 ES6 为目标时,Typescript 编译器缺少 ECMAScript 5 支持
【发布时间】:2020-09-30 10:20:50
【问题描述】:

这是我的 tsconfig.json 文件:(它正在读取,因为之前缺少“异步”的错误已通过添加它来解决)

{
  "compilerOptions": {
    "lib": [ "es6","dom" ]
  }
}

但我收到如下错误:

src\main.ts(85,20): error TS2494: Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher.
src\main.ts(88,38): error TS2569: Type '""' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

扩展运算符或在字符串上使用 for..in 都不起作用:

  var alphas:string[]; 
  const text = "cABCDEFGHIJKLMNOPQRSTUVWXYZ";

  for (const char of text) {
    alphas.push(char);
  }
  
  const availableGlyphs:string[] = [...'cABCDEFGHIJKLMNOPQRSTUVWXYZ']; 
  const glyph = availableGlyphs[Math.floor(Math.random()*availableGlyphs.length)];

【问题讨论】:

  • 请同时添加引发错误的代码
  • 目标是什么?
  • @Amir-Mousavi 你错过了吗?在标题 'targeting ES6' 和文本中它说 "lib": [ "es6","dom" ]
  • 通过添加 es6 库,您的代码不会编译为 es6。
  • libtarget 不同。这是两个独立的配置选项

标签: typescript ecmascript-6 tsc


【解决方案1】:

编辑实际上真正的问题是我混淆了libtarget(感谢@ritaj)。直接的解决方案是这样做:

{
  "compilerOptions": {
    "lib": ["es6", "dom"],
    "target": "es6",
...

【讨论】:

    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 2017-03-14
    • 2014-09-30
    • 2019-01-05
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多