Report #75592
[bug\_fix] Property 'gtag' does not exist on type 'Window & typeof globalThis'.
Create a type declaration file \(e.g., src/types/global.d.ts\) that is included in the TypeScript project \(verify tsconfig.json 'include' array covers this path\). Inside the file, write: declare global \{ interface Window \{ gtag: \(...args: any\[\]\) => void; \} \} export \{\};. The 'export \{\}' makes the file a module, allowing the 'declare global' syntax to augment the global scope. If you omit 'export \{\}', the file is a script, and you must not wrap the interface in 'declare global'; simply declaring 'interface Window \{ ... \}' will perform ambient declaration merging.
Journey Context:
You integrate Google Analytics into a React application. The tracking snippet loads gtag.js into the global window object at runtime. In your analytics utility file, you attempt to call it: window.gtag\('event', 'page\_view'\). TypeScript immediately reports: 'Property gtag does not exist on type Window'. You search for solutions and find that you need to augment the global Window interface. You create a file src/types/gtag.d.ts. Initially, you write: interface Window \{ gtag: any; \}. You save, but the error persists. You realize the file might not be included in the compilation. You check tsconfig.json and see 'include': \['src/\*\*/\*'\], which should cover it. You then read the TypeScript handbook on declaration merging and realize that because your file has no imports or exports, it is treated as a script, not a module. In a script, declaring an interface with the same name as a global interface merges with it, but this can be flaky if there are conflicting definitions. You find a Stack Overflow answer suggesting to add 'export \{\}' to make it a module, then use 'declare global'. You modify the file to: export \{\}; declare global \{ interface Window \{ gtag: \(...args: any\[\]\) => void; \} \}. Immediately, the red squiggly under window.gtag disappears in VS Code. The fix works because the 'export \{\}' turns the file into an external module, which has its own scope. The 'declare global' syntax explicitly lifts the interface augmentation into the global namespace, merging your gtag property into the existing Window interface declared in lib.dom.d.ts. TypeScript now recognizes window.gtag as a valid property everywhere in your project.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:28:37.982398+00:00— report_created — created