简体   繁体   中英

Augmenting global in TypeScript

How can I add fetch onto the global object in TypeScript so I don't have to do this:

(global as any).fetch

I've tried this in a file in ./types/index.d.ts

And tried to reference it by including it in the tsconfig.json.

If you use Go to Definition on global you will see it declared in node definition files as :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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