【问题标题】:error TS2304: Build:Cannot find name 'Iterable' after upgrading to Angular 4错误 TS2304:构建:升级到 Angular 4 后找不到名称“Iterable”
【发布时间】:2018-01-03 18:51:12
【问题描述】:

我正在使用 Typescript 2.4.1 并在我的项目中升级了许多包。其中我将 Angular 从 2 升级到 4.3.1。 在@types 包中进行了多次更正后,我留下的错误是:

\node_modules\@types\jquery\index.d.ts(2955,63): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(767,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(771,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(775,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(779,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(783,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(787,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(791,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(795,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(799,24): error TS2304: Build:Cannot find name 'Iterable'.

我发现了许多类似的问题和答案,流行的解决方案是针对“es2015”和/或添加 lib:[“dom”、“es2015”、“es2015.iterable”]。 我已经尝试了所有这些以及更多,但仍然出现相同的“Iterable”错误。

我更新后的 tsconfig.json 是这样的:

{
    "compileOnSave": true,
    "compilerOptions": {
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [ "dom", "dom.iterable", "es2015", "es2015.iterable", "esnext" ],
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "noEmitOnError": true,
        "outDir": "./wwwroot/js",
        "removeComments": false,
        "rootDir": "./Client",
        "sourceMap": true,
        "suppressImplicitAnyIndexErrors": true,
        "target": "es2015",
        "typeRoots": [
            "./node_modules/@types",
            "./Client"
        ]
    },
    "exclude": [
        "node_modules",
        "wwwroot/lib"
    ],
    "filesGlob": [
        "./Client/**/*.ts"
    ]
}

我的 package.json 是:

{
    "version": "1.0.0",
    "name": "web-server",
    "private": true,
    "dependencies": {
        "@angular/common": "^4.3.1",
        "@angular/compiler": "^4.3.1",
        "@angular/core": "^4.3.1",
        "@angular/forms": "^4.3.1",
        "@angular/http": "^4.3.1",
        "@angular/platform-browser": "^4.3.1",
        "@angular/platform-browser-dynamic": "^4.3.1",
        "@angular/router": "^4.3.1",
        "@angular/upgrade": "^4.3.1",
        "bootstrap": "3.3.7",
        "core-js": "2.4.1",
        "lodash": "4.17.4",
        "pixi.js": "4.5.4",
        "reflect-metadata": "0.1.10",
        "rxjs": "^5.4.2",
        "systemjs": "0.20.17",
        "three": "0.86.0",
        "zone.js": "0.8.14"
    },
    "devDependencies": {
        "@types/chai": "^4.0.1",
        "@types/jquery": "3.2.9",
        "@types/lodash": "4.14.71",
        "@types/mocha": "2.2.41",
        "@types/pixi.js": "4.5.2",
        "@types/three": "0.84.19",
        "bower": "^1.8.0",
        "gulp": "3.9.1",
        "gulp-clean": "^0.3.2",
        "gulp-concat": "^2.6.1",
        "gulp-typescript": "^3.2.1",
        "gulp-inline-ng2-template": "^4.0.0",
        "gulp-sourcemaps": "^2.6.0",
        "gulp-tsc": "^1.3.2",
        "gulp-uglify": "^3.0.0",
        "merge2": "^1.1.0",
        "path": "^0.12.7",
        "rimraf": "^2.6.1",
        "systemjs-builder": "^0.16.9",
        "typescript": "2.4.1"
    },
    "scripts": {
        "gulp": "gulp",
        "rimraf": "rimraf",
        "bundle": "gulp bundle",
        "postbundle": "rimraf dist"
    }
}

为什么在所有这些 lib 包含之后找不到“可迭代”? Typescript 编译器不会忽略我的 tsconfig.json,因为更改某些选项会提供各种输出,但没有一个没有错误。

我的环境是带有 Typescript Tools 2.4.1 的 Visual Studio 2015 update 3。 我正在使用 npn @types(没有打字)。 详细的编译输出表明 Visual Studio 有效地使用了 2.4.1 版本。

最奇怪的是,使用 gulp 编译使用相同的 Typescript 版本和 tsconfig 不会出错。

【问题讨论】:

  • 您确定您的应用程序中没有多余的 tsconfig.json 或 tsconfig.app.json 文件覆盖其中的一个或多个设置?曾经发生在我身上...
  • 是的。我终于在没有升级 typescript 的情况下跳到了 VS2017(我最初并不想要),现在一切都很好。

标签: angular typescript iterable


【解决方案1】:

'Iterable' 在 node.js 的类型文件中定义。安装为我解决问题的文件(node_modules/@types/node/index.d.ts)。

npm install @types/node --save-dev

【讨论】:

  • 这也是对我有用的解决方案。问题似乎是来自 absolutetyped.org 的 jQuery 类型文件的当前版本(在撰写本文时)包含以下行:interface JQuery<TElement extends Node = HTMLElement> extends Iterable<TElement> 它引用了 Node,但它实际上并没有引用 node.d.ts 类型定义.
  • @BradWestness,这正是原因。感谢您终于找到问题的根本原因。
  • 你是我的英雄。在我发现这个之前,我正要放弃 Three.JS Typescript。
  • 显然,Iterable 是 ES2016 的一部分,所以您只需将 ES2016 添加到 lib。
【解决方案2】:

我发现,在任何类型的重大更新或更改(例如升级 Angular)之后,可能有多种原因开始看到这些错误。

我发现修复和识别任何问题的最简单方法是手动清理任何构建和配置文件,重新安装节点模块,然后重新构建项目。

首先删除以下内容:

./dist
./node_modules

删除package-lock.json package.json

运行以下命令:

ng update
npm update
npm install

这将确保您的 Angular 项目都是最新的,请密切关注输出,因为它可能会列出编译期间未出现的问题,Angular 编译器有时会产生误导。

最后重建你的 Angular 项目:

ng build

应该识别任何问题并确保重建所有路径、配置等。

注意:如果您使用ng build --watch 或似乎无法识别编译器错误,需要注意一件事,事情可能会在后台挂起,我注意到了也可以有一个目录%40theme,我认为它实际上是/dist 的副本。此目录不会显示在您的解决方案资源管理器中,如果您开始遇到这些编译器错误,请尝试查找不应该存在的目录和文件并将其删除,清理项目,重新打开 VS 并重新运行ng build

如果事情仍然完全没有问题,您可以随时尝试完全的蛮力清理以尝试修复或发现问题:

删除上面列出的目录,然后在您的 VS 项目中删除以下内容:

<Project>/bin/*
<Project>/obj/*

这些是 VS 保存已编译的 .dll 文件和其他资源的地方,这将迫使 VS 重新构建所有内容。

清理您的解决方案,然后尝试重建整个解决方案。

【讨论】:

  • 谢谢!这是经过大量搜索后唯一有效的方法
【解决方案3】:

最好的解决方案是消除不推荐使用的类型并安装你自己真正需要的类型作为@types

如果您有复杂的输入,这是一个很好的指南:

https://georgedyrra.com/2017/06/04/migrating-from-typings-to-npm-types/

这是我解决的配置。使用 npm install 从 angular 2 升级到 5 确实很有帮助,而不是尝试手动编辑 package.json 文件。

Typings seems utterly messed up after converting project from angular 2 to angular 5

【讨论】:

    【解决方案4】:

    最近在谷歌搜索我的问题时遇到了这个问题,但这些都没有解决我的问题。我不得不进行一些挖掘,但最终发现我必须将 es6 定义文件包含在我的根 typescript 文件的顶部:

    ///<reference path="./node_modules/typescript/lib/lib.es6.d.ts"/> 
    

    【讨论】:

      【解决方案5】:

      更新:以下解决方案对我造成了部署问题。更好的解决方案是仅从解决方案中排除 node_modules 文件夹中的所有 tsconfig.json 文件。似乎与this Visual Studio 问题有关。

      我在使用相同版本的 Visual Studio 和 TypeScript 时遇到了类似的问题。 Visual Studio Code 中的一切都按预期工作。我尝试了其他发布的建议无济于事......唯一修复它的是从我的项目中排除 node_modules 文件夹,但将其保留在解决方案中。

      【讨论】:

        【解决方案6】:

        升级我的整个环境,而不是项目,成功了。 我升级到 VS2017,现在一切正常。

        我的安装中可能存在损坏,尽管我重新安装了除了 VS2015 本身之外的几乎所有东西。

        感谢大家的帮助。

        【讨论】:

          【解决方案7】:

          这看起来和这里描述的很像:

          https://github.com/DefinitelyTyped/DefinitelyTyped/issues/16939

          所以要么尝试使用

          "target": "es6"
          

          如那里所述 - 或对我们有用的方法:

          "target": "es5",
          "lib": ["es2016","dom"]
          

          【讨论】:

          • 我已经看到这篇文章并尝试了您的两种解决方案,但结果相同。顺便说一句,“es6”应该和“es2015”一样。
          • 将 ES2016 添加到 lib 就足够了,这对我有用。
          猜你喜欢
          • 2019-07-12
          • 2019-12-03
          • 1970-01-01
          • 2017-10-03
          • 2016-12-18
          • 2019-05-06
          • 2019-08-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多