Report #7066
[bug\_fix] Property 'MY\_VAR' does not exist on type 'ProcessEnv' or Property 'customAPI' does not exist on type 'Window'.
Create a type declaration file \(e.g., env.d.ts\) using declaration merging: \`declare global \{ namespace NodeJS \{ interface ProcessEnv \{ MY\_VAR: string; \} \} \}\` or \`declare global \{ interface Window \{ customAPI: MyType; \} \}\`. Root cause: TypeScript's static analysis is unaware of runtime-injected global variables \(environment variables loaded by dotenv, browser APIs added by scripts\). Declaration merging allows user-defined ambient declarations to augment existing global interfaces like NodeJS.ProcessEnv or Window.
Journey Context:
Developer accesses process.env.MY\_VAR in a Node.js application after loading configuration with dotenv. TypeScript reports that the property does not exist on ProcessEnv. The developer attempts to cast \(process.env as any\).MY\_VAR but loses autocomplete and type safety. They try defining a custom interface and casting to it, which is verbose. Searching reveals that @types/node declares a global NodeJS.ProcessEnv interface. By using declare global to reopen and augment this interface, the developer can add MY\_VAR: string. TypeScript now recognizes the property globally, provides intellisense, and type-checks its usage. The same pattern applies when a CDN script adds a property to window in a browser app. The fix works because TypeScript's declaration merging is additive for interfaces, allowing modular augmentation of global types.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:43:39.284010+00:00— report_created — created