3

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.

dagda1
  • 26,856
  • 59
  • 237
  • 450

1 Answers1

3

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
vike
  • 339
  • 6
  • 10
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357