【问题标题】:WebStorm - Using Spread Operator Returns: error TS2488: Type '{}' must have a '[Symbol.iterator]()' method that returns an iteratorWebStorm - 使用展开运算符返回:错误 TS2488:类型“{}”必须具有返回迭代器的“[Symbol.iterator]()”方法
【发布时间】:2019-04-28 23:35:23
【问题描述】:

我遇到了这个恼人的 TypeScript 错误:

错误 TS2488:类型“{}”必须具有返回迭代器的“Symbol.iterator”方法。

正如您在我的代码中看到的,我使用的是扩展运算符。经过一些研究,我发现我必须将 mytsconfig.json 更新为 "target": "es6" 但没有任何改变。我错过了什么?

功能

 formatFileName(e) {
    let files = e.target.files;
    this.test = Array.from(files).reduce((acc, cur) => [
        ...acc, {
            name: cur.name.replace(/^.*\\/, "")
        }
    ], [])
 }

TS 配置

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "typeRoots": [
        "node_modules/@types"
    ],
    "lib": [
        "es2017",
        "dom"
    ]
   }
 }

TS CONFIG APP(如果有帮助)

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": [],
    "moduleResolution": "node",
    "target": "es6",
  },
 "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

【问题讨论】:

  • 无法使用您提供的tsconfig 重现您的问题
  • 所以你的意思是问题不在我的tsconfig?也许是我正在使用的 Webstorm?
  • 不确定问题出在哪里。我会尝试在 IDE 之外运行编译器,看看是否是 IDE 问题。我在 VSCode 中使用了你的 tsconfig,它似乎可以工作
  • 好的,我已经更新了问题,表明它可能是由 Webstorm 引起的。我也试试你的建议,谢谢

标签: angular typescript ecmascript-6 webstorm


【解决方案1】:

我解决了这个问题。我注意到在'lib' tsconfig.json 文件中没有指出“es2015”(如this 答案中所建议的那样),但是由于某种原因,这并没有改变任何东西。

所以我使用函数 'formatFileName(e)' 并分配给 this.test 文件数组,然后在声明之后我应用了 reduce 函数来执行我的操作。

固定功能

 formatFileName(e) {
    let files = e.target.files;
    this.test = Array.from(files)
    this.test.reduce((acc, cur) => [
        ...acc, {
            name: cur.name.replace(/^.*\\/, "")
        }
    ], [])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多