How to explicitly add a new property to the global `window` object in TypeScript?
Hi everyone! I've been writing TypeScript recently and ran into a problem. I need to store some global data in `window` so it's accessible from different modules. For example, I want to add my `myAppConfig` there:
```typescript
window.myAppConfig = {
apiUrl: 'https://api.example.com',
debugMode: true
};
```
But TypeScript throws an error:
```
Property 'myAppConfig' does not exist on type 'Window & typeof globalThis'.
```
I understand that TypeScript knows the standard structure of `window` and complains about a non-existent property. How do I properly declare a new property? I've read about extending interfaces but haven't fully figured it out. Please advise the simplest and most correct way. Thanks in advance!
<<>>
Properly extending the Window interface
The problem
No answers yet.
Log in to leave an answer
Blogs