在 node.js 中扩展 TypeScript 全局对象

Extending TypeScript Global object in node.js

我有一个 node.js 应用程序,它将一些配置信息附加到 global 对象:

global.myConfig = {
    a: 1,
    b: 2
}

TypeScript 编译器不喜欢这样,因为 Global 类型没有名为 myConfig:

的对象

TS2339: Property 'myConfig' does not exist on type 'Global'.

我不想这样做:

global['myConfig'] = { ... }

如何扩展 Global 类型以包含 myConfig 或只是告诉 TypeScript 闭嘴并相信我?我更喜欢第一个。

我不想更改 node.d.ts 中的声明。我看到了这个 SO post 并尝试了这个:

declare module NodeJS  {
    interface Global {
        myConfig: any
    }
}

作为扩展现有Global接口的一种方式,但似乎没有任何效果。

I saw this SO post and tried this:

你可能有类似 vendor.d.ts 的东西:

// some import 
// AND/OR some export

declare module NodeJS  {
    interface Global {
        spotConfig: any
    }
}

您的文件需要 干净 任何根级别 importexports。这会将文件变成 模块 并将其与 全局类型声明命名空间 断开连接。

更多:https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

为了避免 Typescript 声明这样的内容:

TS2339: Property 'myConfig' does not exist on type 'Global'.

我建议定义自定义类型。我在项目的 src/types/custom.d.ts 文件下进行:

declare global {
  namespace NodeJS {
    interface Global {
        myConfig: {
          a: number;
          b: number;
        }
    }
  }
}

然后我确保 tsconfig.json 文件中的 Typescript 考虑了这些:

{
  ...
  "files": [
    ...
    "src/types/custom.d.ts"
  ]
}

现在您可以安全地使用自定义 属性:

console.log(global.myConfig.a);

将以下文件放入我们项目的根目录中。

global.d.ts

declare namespace NodeJS {
  export interface Global {
    myConfig: any
  }
}

我们正在使用 "@types/node": "^7.0.18" 和 TypeScript Version 2.3.4。我们的 tsconfig.json 文件如下所示:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6"  
    },
    "exclude": [
        "node_modules"
    ]
}

唯一对我有用的是:

// lib/my.d.ts

import Global = NodeJS.Global;
export interface CDTGlobal extends Global {
  cdtProjectRoot: string
}

然后像这样在其他文件中使用它

import {CDTGlobal} from "../lib/my.d.ts";
declare const global: CDTGlobal;

const cwd = global.cdtProjectRoot; // works

Copy my answer of :

globalThis就是未来

// Way 1
var abc: number
globalThis.abc = 200 // no error

// Way2
declare var age: number
globalThis.age = 18 // no error

对我有用的是:

declare global {
  module NodeJS {
    interface Global {
      myConfig: any;
    }
  }
}

global.myConfig = 'it works!';

唯一的缺点是使用它时你必须关闭 ESLint 规则 @typescript-eslint/no-namespace

为了完整起见,这里是我的 tsconfig.json:

{
  "compilerOptions": {
    "declaration": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "lib": ["dom", "es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "dist",
    "removeComments": true,
    "resolveJsonModule": true,
    "rootDir": "src",
    "sourceMap": true,
    "strict": true,
    "target": "es6"
  },
  "exclude": ["dist", "node_modules"]
}

使用 'namespace' 而不是 'module' 来声明自定义 TypeScript

如果您使用上述任何答案并且使用的是较新版本的 Typescript,您将会收到关于使用 “模块” 的唠叨。您应该考虑命名空间。

为了满足此处的要求,您实际上需要的不仅仅是扩展 Global 接口。如果您希望直接从 "globalThis" 上下文访问它,您还需要创建一个具有该类型的常量。

注意: 当 OP 询问对象文字时,过程与您在下面看到的相同。而不是 "Debug" 类型是一个函数,您只需根据需要定义接口,然后将 "debug:" 更改为 myConfig 或任何您想要的愿望.

// typically I'll store the below in something like "typings.d.ts"
// this is because, at least typically, these overrides tend to
// be minimal in nature. You could break them up and Typescript
// will pick them up if you wish.

// Augmentations for the global scope can only be directly nested 
// in external modules or ambient module declarations.
export {}

declare global {
  // Definition or type for the function.
  type Debug = (label: string) => (message: any, ...args: any[]) => void

  // If defining an object you might do something like this
  // interface IConfig { a: number, b: number }

  // Extend the Global interface for the NodeJS namespace.
  namespace NodeJS {
    interface Global {
      // Reference our above type, 
      // this allows global.debug to be used anywhere in our code.
      debug: Debug
    }
  }
  
  // This allows us to simply call debug('some_label')('some debug message')
  // from anywhere in our code.
  const debug: Debug
}

如何使用以上内容

为了本示例的完整性,我们所做的只是定义一个全局变量,以便我们可以记录一条简单的调试消息。下面是我们如何将方法绑定到我们的全局上下文。

global.debug = (label: string) => (message: any, ...args: any[]) => console.log(message, ...args)

我们也可以直接调用我们的全局调试方法:

debug('express')(`${req.method}: ${req.url}`)

node@16 开始 NodeJS.Global 界面 has been removed in favor of globalThis

您可以在模块文件中声明新的全局变量:

declare global {
  var NEW_GLOBAL: string;
}

并且在非模块文件中(无顶层import/export)为:

declare var NEW_GLOBAL: string;

重要提示:变量必须声明为varletconst 变量未显示在 globalThis.

我可以获得类型检查和代码智能。

declare namespace NodeJS {
  interface Global {
    property: string
  }
}

但是接口 Global 指向 global.GLOBAL

你能得到正确的类型检查,是因为:

declare var global: NodeJS.Global & typeof globalThis;

但是使用 global.property 无法获得更好的代码智能,除非使用 global.GLOBAL.property

因此,您需要同时定义全局变量 global 和扩展接口 Global

// myglobal.d.ts
declare namespace NodeJS {
  interface Global {
    property: string
  }
}
declare var global: NodeJS.Global & typeof globalThis

现在您可以在键入 global.

时获得 property 情报