Report #10811
[bug\_fix] Property 'myCustomProperty' does not exist on type 'Window' or 'globalThis' despite declaring it in a .d.ts file
TypeScript uses declaration merging to augment global interfaces, but this only works if the augmentation is placed in the global scope. If the file containing the augmentation is treated as a module \(has any \`import\` or \`export\` statement at the top level\), the augmentation becomes local to that module and does not affect the global scope. To fix: \(1\) If the file must remain a module \(e.g., it imports types\), wrap the augmentation in \`declare global \{ interface Window \{ myCustomProperty: string; \} \}\`. \(2\) If the file is a script \(no imports/exports\), ensure the interface declaration is at the top level without \`declare global\`. Additionally, ensure the .d.ts file is included in the TypeScript project via the \`files\` or \`include\` array in tsconfig.json; files outside the project scope are not loaded.
Journey Context:
Developer needs to attach a configuration object to the global \`window\` for legacy reasons. They create a file \`src/types/global.d.ts\` and write \`interface Window \{ myConfig: \{ apiUrl: string \}; \}\`. In their application code \`src/main.ts\`, they write \`console.log\(window.myConfig.apiUrl\)\`, but TypeScript reports 'Property myConfig does not exist on type Window'. The developer first suspects a caching issue and restarts the TS server, to no avail. They then try adding an \`import\` statement to the top of \`global.d.ts\` to reference a type, which inadvertently converts the file into a module, causing the augmentation to be scoped locally and completely breaking the global merge. They then discover the \`declare global \{ ... \}\` syntax and wrap their interface inside it, but still see the error. Finally, they check \`tsconfig.json\` and realize the \`include\` array only covers \`src/\*\*/\*\`, but .d.ts files are sometimes excluded if not explicitly included, or the file might be excluded by a \`files\` array that lists specific files. Adding the global.d.ts path to the \`files\` array or ensuring \`include\` covers it resolves the issue.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:44:36.601517+00:00— report_created — created