0

In a project I have several type declarations of global variables, like so:

declare const process: { [key: string]: any };
declare function setTimeout(...);
...

I am looking to declare a variable named "global" that refers to the global object (global in CommonJS) whose type is implied by all of the other global declarations in the project.

declare const global: ???

Specifically, I'd like for TypeScript to infer that global.process is an object and global.setTimeout is a function because those global variables are declared to be of those types.

Is there a way to get the type of the global object in TypeScript?

ide
  • 19,942
  • 5
  • 64
  • 106
  • 1
    Take a look how it´s done here for the window object: https://stackoverflow.com/a/12709880/5291195 – Marcel Sep 11 '18 at 20:39

1 Answers1

0

I don't think there's a way for Typescript to infer it. If you're looking for specific members in global and know their types you can do it "manually". For example:

declare const global: {
    process: object, 
    setTimeout: (callback:(args:any[])=>void, duration:number)
};
Mattia
  • 2,251
  • 1
  • 22
  • 27