2016-03-03 29 views
14

我使用的是打字稿1.7.5,類型爲0.6.9和角度爲2.0.0-beta.0。由於打字造成的TypeScript編譯錯誤

由於類型定義文件,我該如何擺脫打字稿編譯錯誤信息Duplicate identifier

Duplicate identifier錯誤發生在以下目錄的定義文件:

node_modules/angular2/typings/es6-shim/es6-shim.d.ts 
node_modules/angular2/typings/jasmine/jasmine.d.ts 
node_modules/angular2/typings/zone/zone.d.ts 
typings/browser/ambient/es6-promise/es6-promise.d.ts 
typings/browser/ambient/es6-shim/es6-shim.d.ts 
typings/browser/ambient/jasmine/jasmine.d.ts 
typings/browser/ambient/karma/karma.d.ts 
typings/browser/ambient/zone.js/zone.js.d.ts 

什麼是編譯器node_modules/angular2目錄做的,因爲我在tsconfig.json排除在外呢?

I also posted this question on GitHub

tsconfig.json

{ 
    "compilerOptions": { 
     "target": "es5", 
     "module": "system", 
     "moduleResolution": "node", 
     "sourceMap": true, 
     "emitDecoratorMetadata": true, 
     "experimentalDecorators": true, 
     "removeComments": false, 
     "noImplicitAny": false 
    }, 
    "exclude": [ 
     "node_modules", 
     "typings/main", 
     "typings/main.d.ts" 
    ] 
} 

如果我改變的tsconfig.jsonexclude一部分,他們都走了:

"exclude": [ 
    "node_modules", 
    "typings" 
] 

但後來加入以下後,我再次得到相同的Duplicate identifier編譯錯誤:

/// <reference path="../../typings/browser.d.ts" /> 

typings.json

{ 
    "name": "example-mean-app-client", 
    "dependencies": {}, 
    "devDependencies": {}, 
    "ambientDependencies": { 
    "bootstrap": "github:DefinitelyTyped/DefinitelyTyped/bootstrap/bootstrap.d.ts#4de74cb527395c13ba20b438c3a7a419ad931f1c", 
    "es6-promise": "github:DefinitelyTyped/DefinitelyTyped/es6-promise/es6-promise.d.ts#830e8ebd9ef137d039d5c7ede24a421f08595f83", 
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#4de74cb527395c13ba20b438c3a7a419ad931f1c", 
    "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#dd638012d63e069f2c99d06ef4dcc9616a943ee4", 
    "karma": "github:DefinitelyTyped/DefinitelyTyped/karma/karma.d.ts#02dd2f323e1bcb8a823269f89e0909ec9e5e38b5", 
    "karma-jasmine": "github:DefinitelyTyped/DefinitelyTyped/karma-jasmine/karma-jasmine.d.ts#661e01689612eeb784e931e4f5274d4ea5d588b7", 
    "systemjs": "github:DefinitelyTyped/DefinitelyTyped/systemjs/systemjs.d.ts#83af898254689400de8fb6495c34119ae57ec3fe", 
    "zone.js": "github:DefinitelyTyped/DefinitelyTyped/zone.js/zone.js.d.ts#9027703c0bd831319dcdf7f3169f7a468537f448" 
    } 
} 

回答

3

至於basarat暗示,您可以更改:

"moduleResolution": "node", 

"moduleResolution": "classic", 

或者你可以簡單地刪除其中的所有文件夾分型重複分型的。現在發生的事情是,它會自動導入您在代碼中執行的每個import的node_modules文件夾中的所有類型。它還導入了與browser.d.ts文件相關的類型。

+0

設置「moduleResolution」:「經典「會提供其他錯誤,但由於未找到要導入的模塊。 我不知道「node_modules/angular2/typing」和「typings」目錄中有重複的類型。刪除重複類型解決了問題 –

+0

這個問題解決了嗎?我遇到了同樣的問題。我已經參加了一個禮拜,沒有任何解決方案對我有幫助。 –

+0

如果你有完全相同的問題,那麼問題是他已經安裝了Angular 2已經包含的一些類型。這會導致重複。解決方案是卸載重複項。 – rgvassar

3

什麼是編譯器在node_modules/angular2目錄做,因爲我排除它tsconfig.json

它看着NPM模塊監守的"moduleResolution": "node",只有被導入的文件(沒有排除它會看到全部的文件)。

4

對於我來說,選擇是「瀏覽器」或「主」(取決於您的應用程序:前端或後端),但不包括在tsconfig.json另外一個工作:

"exclude": [ 
    "node_modules", 
    "wwwroot", 
    "typings/main", 
    "typings/main.d.ts" 
    ] 
+0

這個作品。謝謝:) – Sampath

+3

有人可以擴展這個嗎?該解決方案是否適用於使用Angular2的一般情況? – blong