简体   繁体   English

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

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

I have a barebones project on this github with the following basic structure: 我在这个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"
  }
}

But in spite of the index.d.ts file, Typescript still returns an error saying [ts] Property 'browser' does not exist on type 'Process'. 但是尽管有index.d.ts文件,但Typescript仍然返回错误,指出[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. 我认为index.d.ts将在整个项目范围内添加必要的扩展名,但显然没有。

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. 该黑客与以下事实有关: nextjs将浏览器属性附加到该进程,并执行其他一些古怪的事情,需要对服务器的全局名称空间进行polyfill,该全局名称空间会遇到相同的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. 打字稿忽略index.d.tsindex.ts的存在是因为它假定index.d.ts可能从产生index.tsindex.ts更先进的日期。 Renaming index.d.ts to another name (for example, declarations.d.ts ) will fix the problem. 重命名index.d.ts到另一个名称(例如, declarations.d.ts )会解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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