Skip to content

chore(types): declare typed ImportMetaEnv for VITE_ vars#360

Closed
yunus-dev-codecrafter wants to merge 1 commit into
Grainlify:mainfrom
fikrah-Tech:chore/typed-vite-env
Closed

chore(types): declare typed ImportMetaEnv for VITE_ vars#360
yunus-dev-codecrafter wants to merge 1 commit into
Grainlify:mainfrom
fikrah-Tech:chore/typed-vite-env

Conversation

@yunus-dev-codecrafter

Copy link
Copy Markdown
Contributor

closes #305

chore(types): declare typed ImportMetaEnv for VITE_ vars

Why

src/shared/config/api.ts reads import.meta.env.VITE_API_BASE_URL and VITE_FRONTEND_BASE_URL, but there was no typed ImportMetaEnv declaration. This meant a typo like VITE_API_BSE_URL would compile silently and only fail at runtime.

What

Added TSDoc-documented ImportMetaEnv and ImportMeta augmentations to src/vite-env.d.ts:

interface ImportMetaEnv {
  /** Backend API base URL (required) */
  readonly VITE_API_BASE_URL: string;
  /** Frontend base URL (optional — defaults to window.location.origin) */
  readonly VITE_FRONTEND_BASE_URL?: string;
}

interface ImportMeta {
  readonly env: ImportMetaEnv;
}

Verification

Check Result
npm run typecheckapi.ts ✅ No errors
npm run typechecklogger.ts (uses import.meta.env.PROD) ✅ No errors (merged with vite/client's baked-in ImportMetaEnv)
npm run typecheckvite-env.d.ts ✅ No errors
Undeclared key (e.g. VITE_API_BSE_URL) ✅ Caught at compile time — Property 'VITE_API_BSE_URL' does not exist on type 'ImportMetaEnv'
npm test ⚠️ 77 pre-existing suite failures (all 0 tests, infrastructure issue — unrelated)

Security notes

  • Only public VITE_* environment variables are declared. Server-only secrets (API keys, auth tokens, etc.) must never be added here — they would be exposed to the client bundle.
  • These types cover only what the browser needs; sensitive configuration remains server-side.

Migration

No migration needed. The changes are purely additive type declarations. Existing .env files and runtime behavior are unaffected.

@Jagadeeshftw

Copy link
Copy Markdown
Contributor

hey @yunus-dev-codecrafter, thanks for jumping in! closing this pr because the author needs to be assigned to the linked issue (#305) before opening a pr. please comment on the issue requesting assignment, wait to be assigned, then re-raise your pr. this keeps work fairly distributed across contributors. hope to see a revised pr from you soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a typed Vite env declaration file for VITE_* variables in vite-env.d.ts

2 participants