Add a window property with TypeScript

Define a window property

In my case TypeScript did not know about window.__coverage__ which is a property that is set by istanbul’s instrumenter class.

When trying to use if TypeScript will complain:

TS2339: Property ‘coverage‘ does not exist on type ‘Window’.

To get along with it we need to make TypeScript aware of this new property by declaring it in our TypeScript code:

1
declare global {
2
  interface Window {
3
    __coverage__: Object;
4
  }
5
}

Define a global property

If you want to define a global __coverage__ property in a Node.js environment, you will have to assign it to the global object:

1
declare global {
2
  namespace NodeJS {
3
    interface Global {
4
      __coverage__: {};
5
    }
6
  }
7
}