简体   繁体   中英

cannot extend Process interface with index.d.ts in typescript

I have a barebones project on this github with the following basic structure:

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"
  }
}

But in spite of the index.d.ts file, Typescript still returns an error saying [ts] Property 'browser' does not exist on type 'Process'. I thought that the index.d.ts would add the necessary extension on a project-wide basis, but apparently not.

The hack is related to the fact that nextjs appends a browser property to the process and does a couple other wacky things that require polyfills to the server's global namespace, which runs into the same isuse.

Does anyone know how to accomplish this sort of interface-extending within a project? Thanks!

TypeScript ignores index.d.ts when index.ts exists because it assumes that index.d.ts might be generated from index.ts and index.ts is more up-to-date. Renaming index.d.ts to another name (for example, declarations.d.ts ) will fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM