2017-03-25 101 views
1

我正在將Angular項目(使用Angular CLI創建)從2升級到4.我遵循升級說明,但現在項目不能編譯,由於類似這樣的錯誤一個:從Angular 2升級到4後出現類型錯誤

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

爲了進行調試,我創建了一個新的Angular 4項目,再次與CLI進行比較。新創建的項目無錯地編譯。

當我編譯有問題的文件(lib.dom.d.ts)時,它們完全相同,但在升級的項目中,錯誤在VS Code中突出顯示,但不在新項目中顯示(left is升級後,右邊是新項目):

compare

(有趣的是,我有很多的新項目的滾動區域的紅色指示燈,什麼是他們試圖告訴我的)

我想我可以弄清楚這兩個項目之間的差異是如何解決這個問題的,但我沒有找到任何解決方案。

兩個項目返回

./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" 
    ] 
    } 
} 

所以問題還有什麼可以導致行爲的區別?

+0

檢查core-js的版本是否相同。在tsconfig中刪除「dom」是否可以做任何事情? – pixelbits

回答

0

作爲參考,問題是,我在這兩個dependenciespackage.jsondevDependencies有 '打字稿'。從依賴關係中刪除它解決了這個問題。

1

請確保您的package.json中的typescript版本是2.1.x或更多。

"typescript": "~2.1.0"

看到這個鏈接,如果它有助於:angular2 -> 4

0

搜索者的利益,如果你使用Visual Studio 2015年,您可能還需要「打字稿2.2擴展爲Visual Studio 2015年」到很好地獲得打字稿編譯。

enter image description here

相關問題