Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I try to convert an existing project to use Typescript and I have problems doing so with my testing setup.

I had a setup file for my tests that sets up jsdom so that all my DOM interacting code works during my tests. Using Typescript (ts-node with mocha) I always get errors like this:

Property 'window' does not exist on type 'Global'.

To prevent this I tried patching the NodeJS.Global interface like this:

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

But this didn't change anything.

How do I enable those browser properties on the NodeJS global variable?

Extras:

This is my mocha setup.ts:

import { jsdom, changeURL } from 'jsdom';

const exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = global.document.defaultView;
Object.keys(global.document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    exposedProperties.push(property);
    global[property] = global.document.defaultView[property];
  }
});

global.navigator = {
  userAgent: 'node.js',
};

changeURL(global.window, 'http://example.com/');
like image 470
fahrradflucht Avatar asked Nov 22 '16 13:11

fahrradflucht


4 Answers

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;
    } 
  }
}
like image 171
Steven Spungin Avatar answered Oct 17 '22 06:10

Steven Spungin


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

(global as any).myvar = myvar;
like image 22
lleaff Avatar answered Oct 17 '22 08:10

lleaff


I fixed this problem by doing this...

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

declare var global: Global;
like image 26
Shawn Avatar answered Oct 17 '22 07:10

Shawn


This is the right solution, not using Typescript's namespaces. It is also compatible with all eslint default rules:

// Declare a type.
interface CustomNodeJsGlobal extends NodeJS.Global {
    myExtraGlobalVariable: number;
    // You can declare anything you need.
}

Use it:

// Tell Typescript to use this type on the globally scoped `global` variable.
declare const global: CustomNodeJsGlobal;

function doSomething() {
  // Use it freely
  global.myExtraGlobalVariable = 5;
}

doSomething();
like image 17
José Cabo Avatar answered Oct 17 '22 08:10

José Cabo