Augmenting global in TypeScript

1.9k Views Asked by At

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.

1

There are 1 best solutions below

3
Titian Cernicova-Dragomir On BEST ANSWER

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