Table of contents

  1. 1. Define a window property
  2. 2. Define a global property

Sometimes you want to define a custom property on the window namespace. This can be done by declaring it as a global property.

Define a window property

In the following case we want to make TypeScript aware of the property window.__coverage__ which is set by istanbul’s instrumenter class.

When calling __coverage__ on window the TypeScript compiler complains with the following error:

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

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

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

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 because there is no window in Node.js:

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