網站搜尋

angularjs - 錯誤 TS2300:TypeScript 編譯過程中重複的識別符 'require'


我正在編譯我 Angular 專案中的所有 TypeScript,但我遇到了眾所周知的錯誤:

error TS2300: Duplicate identifier 'require'.

然而,大多數遇到這個問題的人是因為他們有兩個檔案導致了重複錯誤。在我的情況下,原因是在同一個檔案中兩次使用了單詞require。

/typings/globals/angular/index.d.ts(1707,9):

和

/typings/globals/angular/index.d.ts(1717,9): 

它是從 dt 來源提取的預設 Angular 型別定義檔案。

我在本地和全域都安裝了 TypeScript。我嘗試移除本地版本,但那會出現錯誤。這是我的 tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true
  },
  "files": [
    "typings/index.d.ts"
  ]
}

以及我的 typings.json:

{
  "globalDependencies": {
    "angular": "registry:dt/angular#1.5.0+20160627014139",
    "bootstrap": "registry:dt/bootstrap#3.3.5+20160619023404",
    "jquery": "registry:dt/jquery#1.10.0+20160620094458"
  }
}

最後是我的主要 index.d.ts:

/// <reference path="globals/angular/index.d.ts" />
/// <reference path="globals/bootstrap/index.d.ts" />
/// <reference path="globals/jquery/index.d.ts" />

答案#1

你不應該在你的 tsconfig.json 文件的 files 部分列出 d.ts 文件。那個部分是用來列出你已經編寫並需要轉譯成 JavaScript 的文件。你遇到的問題是因為轉譯器試圖將你的 typings/index.d.ts 文件轉換成 JavaScript,而這是它不應該做的。型別定義文件主要是讓你的程式編輯器提供型別提示、警告錯誤,以及代碼補全功能。

看看這個 tsconfig.json 文件的概覽。