How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

Solution 1:

Original Answer To Avoid Error

Put this at the top of your typescript file

const globalAny:any = global;

Then use globalAny instead.

globalAny.document = jsdom('');
globalAny.window = global.document.defaultView;

Updated Answer To Maintain Type Safety

If you want to keep your type safety, you can augment the existing NodeJS.Global type definition.

You need to put your definition inside the global scope declare global {...}

Keep in mind that the typescript global scope is not the same as the NodeJS interface Global, or the node global property called global of type Global...

declare global {
  namespace NodeJS {
    interface Global {
       document: Document;
       window: Window;
       navigator: Navigator;
    } 
  }
}

Solution 2:

In addition to other answers, you can also simply cast global directly at the assignment site:

(global as any).myvar = myvar;

Solution 3:

I fixed this problem by doing this...

export interface Global {
  document: Document;
  window: Window;
}

declare var global: Global;