[英]Node.js global variable and TypeScript
I need to have some strongly-typed global variables.我需要一些强类型的全局变量。
As mentioned here: Extending TypeScript Global object in node.js , in order to add fields to the global
variable I need to add adts file that extends the Global
interface that's specified in node.d.ts.如此处所述: Extending TypeScript Global object in node.js ,为了向
global
变量添加字段,我需要添加扩展 node.d.ts 中指定的Global
接口的 adts 文件。
Also, as Basarat mentioned:另外,正如 Basarat 提到的:
Your file needs to be clean of any root level import or exports.
您的文件需要清除任何根级导入或导出。 That would turn the file into a module and disconnect it from the global type declaration namespace.
这会将文件变成一个模块并将其与全局类型声明命名空间断开连接。
Now, I need to have fields on the Global
interface whose types are custom interfaces that I created:现在,我需要在
Global
接口上有一些字段,这些字段的类型是我创建的自定义接口:
declare namespace NodeJS{
interface Global {
foo: Foo
bar: Bar
}
}
I'm extremely not willing to use the any
type.我非常不愿意使用
any
类型。
I can move/copy all the interface declarations to this declaration file, but it's a bad solution for me, since both Foo and Bar in turn, aggregate many fields of other interfaces, including third party interfaces like Moment etc.我可以将所有接口声明移动/复制到这个声明文件,但这对我来说是一个糟糕的解决方案,因为 Foo 和 Bar 反过来聚合了其他接口的许多字段,包括 Moment 等第三方接口。
I need a solution for this paradox我需要一个解决这个悖论的方法
Here's an approach.这是一个方法。 I don't know if this is the 'correct' way of doing things, but it works for me with TypeScript 3.7.4.
我不知道这是否是“正确”的做事方式,但它适用于 TypeScript 3.7.4。
src
, create a new folder src/types
and create a file global.d.ts
in this folder.src
文件夹中,请创建一个新文件夹src/types
并在此文件夹中创建一个文件global.d.ts
。import { Express } from 'express';
declare global {
namespace NodeJS {
interface Global {
__EXPRESS_APP__: Express;
}
}
}
declare namespace NodeJS {
interface Global {
__CONNECTION_COUNT__: number;
}
}
global.d.ts
file (and any other files you might add to src/types
) is picked up by the TypeScript compiler, by adding the following to your tsconfig.json
file:tsconfig.json
文件,确保您的global.d.ts
文件(以及您可能添加到src/types
任何其他文件)被 TypeScript 编译器tsconfig.json
:{
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
}
// Below, `app` will have the correct typings
const app = global.__EXPRESS_APP__;
I found this works.我发现这有效。
Have one file that declares the property on the NodeJS.Global interface with the any type.有一个文件,用 any 类型声明 NodeJS.Global 接口上的属性。 This file has to be clean of imports or refrences.
该文件必须没有导入或引用。
node.d.ts节点.d.ts
declare namespace NodeJS{
interface Global {
foo: any
}
}
Then in the second file you declare a global variable that has the correct type.然后在第二个文件中声明一个具有正确类型的全局变量。
global.d.ts global.d.ts
import IFoo from '../foo'
declare global {
const foo:Ifoo
}
This worked for me (node v16.13.2)这对我有用(节点 v16.13.2)
types/global.d.ts
types/global.d.ts
declare global {
var __root: string
}
export {}
Note that __root
declared with var
keyword.请注意,
__root
使用var
关键字声明。 It works with let
and const
too, but in this case __root
will have any
type.它也适用于
let
和const
,但在这种情况下__root
将具有any
类型。 I don't know why;) If someone can explain this it will be great.我不知道为什么;)如果有人能对此进行解释,那就太好了。
tsconfig.json
tsconfig.json
{
"compilerOptions": {
"typeRoots": [
"types"
],
}
}
// app.ts (entry point)
import path from 'path'
global.__root = path.join(__dirname)
// anyFileInProject.ts
console.log(__root) // will display root directory
Simple and Easy 简单易行
create a static property in Global
class 在
Global
类中创建一个静态属性
export class Global {
static name: string
}
then use it without hesitation 然后毫不犹豫地使用它
export class MyClass {
constructor() {
Global.name = 'Wasif'
console.log(Global.name)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.