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
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:

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