Report #14968
[bug\_fix] TS2339: Property 'myLib' does not exist on type 'Window' when using declare global in a non-module file
Add 'export \{\};' at the top of the .d.ts file to make it a module, which allows 'declare global' to correctly augment the global scope rather than shadowing it.
Journey Context:
Developer needs to use a legacy library that attaches to the window object \(e.g., 'window.Stripe' or 'window.google'\). They create a 'globals.d.ts' file in their project root with: 'declare global \{ interface Window \{ myLib: any; \} \}'. They restart VS Code, but accessing 'window.myLib' still shows TS2339: 'Property myLib does not exist on type Window'. They check tsconfig.json and confirm 'globals.d.ts' is included in the 'include' array. They try adding 'declare var window: Window & \{ myLib: any \};' but that creates duplicate identifier errors. They try moving the declaration into a 'global.d.ts' without the 'declare global' block, just 'interface Window \{ myLib: any; \}', but this creates a separate interface that doesn't merge with the DOM lib's Window interface. Eventually, they learn that 'declare global' only works correctly inside a module context. If the file has no import or export statements, TypeScript treats it as a script file in the global scope, and 'declare global' either does nothing or behaves unexpectedly. By adding 'export \{\};' \(an empty export\) at the top of the file, the file becomes an ES module, and 'declare global' correctly augments the existing global Window interface from lib.dom.d.ts.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:50:26.232238+00:00— report_created — created