关于oop:在node.js中的嵌套接口中添加属性

Adding properties to a nested interface in node.js

在名为\\'some-file.ts \\'的文件中,我要执行以下操作:

1
global.someProp  = someVar;

,而global是类型为NodeJS.Global的预定义对象,该对象在node.d.ts.

中定义。

如何使该界面局部化?

我尝试了以下所有方法,但均无济于事:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
interface Global {
        someProp: string;
    }

global.someProp  = someVar;



interface NodeJS.Global {
        someProp: string;
    }

global.someProp  = someVar;



namespace NodeJS{
    interface Global {
        someProp: string;
    }
}

global.someProp  = someVar;

我不断得到:

TS339: Property 'someProp' does not exist on type 'Global'

我该如何解决?


我已经解决了。我不得不按如下方式使用关键字" declare":

1
2
3
4
5
declare namespace NodeJS{
    interface Global {
        base_dir: string;
    }
}