在打字稿中增加整体

时间:2019-05-20 14:57:44

标签: typescript

如何将提取内容添加到打字稿中的全局对象上,所以我不必这样做

(global as any).fetch

我已经在./types/index.d.ts的文件中尝试过

并尝试通过将其包含在tsconfig.json中来引用它。

1 个答案:

答案 0 :(得分:0)

如果在global上使用go to definition,则会在节点定义文件中将其声明为:

declare var global: NodeJS.Global; 

因此,要向其中添加内容,您只需要在Global命名空间中扩展NodeJS接口:

declare namespace NodeJS {
    export interface Global {
        fetch(u: string): string
    }
}
global.fetch("") // ok now