Sometimes you will run into cases where a library or other code adds things to the global window
or document
objects and TypeScript will complain if it’s not aware of these properties. This will lead to a dreaded “property does not exist” error from the TypeScript compiler. To fix this you can type them yourself easily!
Let’s say that we have a window.clicks
property that is a number, we can type it like this:
interface Window {
clicks: number;
}
Or let’s say we have a document.items
which is an array of strings, we can type it like this:
interface Document {
items: string[];
}
Now you can happily use them in your code without TypeScript complaining:
console.log(window.clicks);
console.log(document.items);