【问题标题】:Type Errors after upgrading from Angular 2 to 4从 Angular 2 升级到 4 后的类型错误
【发布时间】:2017-04-30 23:57:54
【问题描述】:

我正在将一个 Angular 项目(使用 Angular CLI 创建)从 2 升级到 4。我已按照升级说明进行操作,但现在该项目无法编译,因为这样的类型错误:

ERROR in /mypath/node_modules/typescript/lib/lib.dom.d.ts (6945,11): 
Duplicate identifier 'Headers'.

为了进行调试,我再次使用 CLI 创建了一个新的 Angular 4 项目来进行比较。新创建的项目编译没有错误。

当我编译有问题的文件(lib.dom.d.ts)时,它们完全相同,但是在升级的项目中,错误在VS Code中突出显示,但在新项目中没有(左边是升级的,右边是新项目):

(有趣的是,我在新项目的滚动区域中有很多红色指示器,它们想告诉我什么?)

我想我可以弄清楚这两个项目之间有什么区别来解决这个问题,但我没有找到任何区别。

两个项目都返回

./node_modules/tslint/bin/tslint -v
4.5.1

./node_modules/typescript/bin/tsc -v
Version 2.2.1

ng -v
@angular/cli: 1.0.0
node: 7.4.0
os: darwin x64
@angular/animations: 4.0.0
@angular/common: 4.0.0
@angular/compiler: 4.0.0
@angular/compiler-cli: 4.0.0
@angular/core: 4.0.0
@angular/forms: 4.0.0
@angular/http: 4.0.0
@angular/platform-browser: 4.0.0
@angular/platform-browser-dynamic: 4.0.0
@angular/platform-server: 4.0.0
@angular/router: 4.0.0
@angular/cli: 1.0.0

tsconfig.json 相同:

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

那么问题还有什么可能导致行为差异?

【问题讨论】:

  • 查看core-js的版本是否相同。从 tsconfig 中的“lib”中删除“dom”有什么作用吗?

标签: angular typescript


【解决方案1】:

确保您的 package.json 中的 typescript 版本为 2.1.x 或更高。

"typescript": "~2.1.0"

如果有帮助,请查看此链接:angular2 -> 4

【讨论】:

    【解决方案2】:

    为了搜索者的利益,如果您使用的是 Visual Studio 2015,您可能还需要“Typescript 2.2 Extension for Visual Studio 2015”才能很好地编译 typescript。

    【讨论】:

      【解决方案3】:

      作为参考,问题是我在dependenciesdevDependencies 中都有'typescript' 在package.json 中。从依赖项中删除它解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 2017-10-20
        • 2011-08-17
        • 1970-01-01
        • 2020-05-25
        • 2021-05-22
        • 2011-06-22
        相关资源
        最近更新 更多