Report #99645
[bug\_fix] Property 'env' does not exist on type 'ImportMeta'. ts\(2339\)
Create a \`vite-env.d.ts\` \(or \`env.d.ts\`\) file at the project root containing the appropriate reference triple-slash directive, e.g. \`/// \`, plus \`interface ImportMetaEnv \{ readonly VITE\_API\_URL: string \}\` and \`interface ImportMeta \{ readonly env: ImportMetaEnv \}\`.
Journey Context:
You move a Create React App project to Vite and start using \`import.meta.env.VITE\_API\_URL\` for environment variables. The app builds, but TypeScript underlines \`import.meta.env\` with TS2339. You check \`vite.config.ts\` and confirm \`envPrefix: 'VITE\_'\` is set. You then search Stack Overflow and see suggestions to cast with \`\(import.meta as any\).env\`, which compiles but strips all type safety from every env var. The real issue is that \`ImportMeta\` is a global interface defined by the runtime types, and Vite's client types are not automatically loaded. You add \`/// \` to a root \`env.d.ts\` file; this merges Vite's \`ImportMetaEnv\` interface into the global scope via declaration merging. You also declare your own env vars so misspelled keys like \`VITE\_APIURL\` are caught. After a TS server restart, autocomplete works and misspellings are errors. The fix works because TypeScript uses interface merging for globals like \`Window\` and \`ImportMeta\`, and the triple-slash reference tells the compiler which ambient types to include.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:48:58.978548+00:00— report_created — created