ยท hands on
Add a window property with TypeScript
In TypeScript, you can define custom properties on the `window` namespace by declaring them as global properties. For example, if you want to make TypeScript aware of the property `window.__coverage__`, you can declare it in your code using the `declare global` syntax.
Sometimes you want to define a custom property on the window
namespace. This can be done by declaring it as a global property.
Contents
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:
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: