簡體   English   中英

無法使用打字稿中的index.d.ts擴展Process接口

[英]cannot extend Process interface with index.d.ts in typescript

我在這個github上有一個准系統項目,具有以下基本結構:

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true
  }
}

index.d.ts

declare namespace NodeJS {
    export interface Process {
        browser: boolean;
    }
}

index.ts

const x = process.browser;

package.json

{
  "name": "tsc-interface-ext",
  "version": "0.0.0",
  "main": "index.ts",
  "license": "MIT",
  "devDependencies": {
    "@types/node": "^10.12.5",
    "tslint": "^5.11.0",
    "typescript": "^3.1.6"
  }
}

但是盡管有index.d.ts文件,但Typescript仍然返回錯誤,指出[ts] Property 'browser' does not exist on type 'Process'. 我認為index.d.ts將在整個項目范圍內添加必要的擴展名,但顯然沒有。

該黑客與以下事實有關: nextjs將瀏覽器屬性附加到該進程,並執行其他一些古怪的事情,需要對服務器的全局名稱空間進行polyfill,該全局名稱空間會遇到相同的isuse。

有誰知道如何在項目中完成這種接口擴展? 謝謝!

打字稿忽略index.d.tsindex.ts的存在是因為它假定index.d.ts可能從產生index.tsindex.ts更先進的日期。 重命名index.d.ts到另一個名稱(例如, declarations.d.ts )會解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM