简体   繁体   English

在 TypeScript 中增强全局

[英]Augmenting global in TypeScript

How can I add fetch onto the global object in TypeScript so I don't have to do this:如何将fetch添加到 TypeScript 中的全局对象上,这样我就不必这样做了:

(global as any).fetch

I've tried this in a file in ./types/index.d.ts我已经在./types/index.d.ts的文件中尝试过这个

And tried to reference it by including it in the tsconfig.json.并尝试通过将其包含在 tsconfig.json 中来引用它。

If you use Go to Definition on global you will see it declared in node definition files as :如果您在global上使用Go to Definition ,您将看到它在节点定义文件中声明为:

declare var global: NodeJS.Global; 

So in order to add things to it you just need to augment the Global interface in the NodeJS namespace:所以为了给它添加东西,你只需要在NodeJS命名空间中增加Global接口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM