Avatar ·

How to explicitly add a new property to the window object in TypeScript

Hello everyone! I started learning TypeScript and encountered a problem when working with the global `window` object.\n\nI need to add a custom property to `window`, for example, to store a global session ID or a flag that will be accessible from any script on the page. In regular JavaScript, I would simply write:\n\n```typescript\nwindow.myCustomProperty = 'hello world';\n```\n\nBut in TypeScript, this causes a compilation error:\n\n```\nProperty 'myCustomProperty' does not exist on type 'Window & typeof globalThis'.\n```\n\nI understand that TypeScript is strictly typed, and `window` doesn't have such a property by default. I tried different approaches: using `(window as any).myCustomProperty`, but that kills all type checking. I attempted to create an interface, but I'm not sure how to apply it correctly.