简体   繁体   English

是否有扩展@ types / node / index.d.ts的好方法

[英]Is there a good way to extend @types/node/index.d.ts

process._getActiveHandles() produces an error because not everything was published in @type/nodes/index.d.ts . process._getActiveHandles()产生错误,因为并非所有内容都发布在@type/nodes/index.d.ts I have node v8.11.1 and I used npm install @types/node@9.6.2 . 我有节点v8.11.1,我使用了npm install @types/node@9.6.2 Should I stop using npm's @types and edit my own copy? 我应该停止使用npm的@types并编辑自己的副本吗?

The @types/node@12.7.5 does not have it in globals.d.ts either. @types/node@12.7.5globals.d.ts也没有。

(<any>process)._getActiveHandles() of course ignores the error, but I don't want to lose the typo-checking. (<any>process)._getActiveHandles()当然会忽略该错误,但我不想丢失拼写检查。

_getActiveHandles() is just one case. _getActiveHandles()仅是一种情况。

You should be able to add custom types to the existing node types. 您应该能够将自定义类型添加到现有节点类型。 With something like: 用类似的东西:

declare global {
  namespace NodeJS {
    interface Process {
      _getActiveHandles(arg: number): boolean;
    }
  }
}

process._getActiveHandles = (arg) => !!arg;
const result = process._getActiveHandles(1)
console.log(result)

Thanks skovy. 谢谢斯科维。 (I could not get code into the comment.) (我无法将代码添加到注释中。)

Placing this in a separate ts file allowed access to the existing function without complaints from both tsc and Visual Code (although tsc might never have complained???): 将其放在单独的ts文件中可以访问现有功能,而不会受到tsc和Visual Code的抱怨(尽管tsc可能从未抱怨过??):

declare global {
    namespace NodeJS {
        interface Process {
            _getActiveHandles(): any[];
        }
    }
}

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

相关问题 node_modules/@types/chrome/index.d.ts 不是模块 - node_modules/@types/chrome/index.d.ts is not a module 如何修复“@types/node/index.d.ts 不是模块”? - How to fix "@types/node/index.d.ts is not a module"? @types/googlemaps/index.d.ts' 不是模块 - @types/googlemaps/index.d.ts' is not a module “module/index.d.ts”和“@types/module/index.d.ts”之间的打字稿冲突 - Typescript conflict between “module/index.d.ts” and “@types/module/index.d.ts” 无法使用打字稿中的index.d.ts扩展Process接口 - cannot extend Process interface with index.d.ts in typescript 如何修复Angular 2错误的node_modules/@types/systemjs/index.d.ts(333,13)? - How to fix node_modules/@types/systemjs/index.d.ts (333,13) of Angular 2 error? node_modules/@types/chai 文件夹中的 index.d.ts 文件出错 - Error in index.d.ts file in node_modules/@types/chai folder Visual Studio代码:node_modules/@types/googlemaps/index.d.ts&#39;不是模块 - Visual Studio Code: node_modules/@types/googlemaps/index.d.ts' is not a module SPFx Webpart - node_modules/@types/ [prop types] 和 [react] index.d.ts:gulp 构建时加载“错误 TS1005” - SPFx Webpart - node_modules/@types/ [prop types] and [react] index.d.ts: loads of "error TS1005" on gulp build 错误:node_modules/@types/d3-dsv/index.d.ts(159,60):错误 TS1005:“,”预期 - Error: node_modules/@types/d3-dsv/index.d.ts(159,60): error TS1005: ',' expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM