命名空间“NodeJS”没有导出成员“Global”

Ant*_*ños 8 node.js typescript

我曾经使用以下代码定义全局变量:

interface CustomNodeJSGlobal extends NodeJS.Global {
  myGlobalVariable: unknown
}

export { CustomNodeJSGlobal }
Run Code Online (Sandbox Code Playgroud)

在 Node 14 中,但是当我安装时@types/node(目前是版本 16),它会抛出一个错误Namespace 'NodeJS' has no exported member 'Global'。如何在 Node 16 及以上版本中声明全局变量?

Dav*_*rdi 5

对于 Node.js >= 16,我认为NodeJS.Globaltype 不再可用,但你可以像这样声明它:

type NodeJSGlobal = typeof global;
Run Code Online (Sandbox Code Playgroud)