-
Notifications
You must be signed in to change notification settings - Fork 6
Infer the import.meta.env type
#415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yamcodes
merged 9 commits into
main
from
195-bug-vite-typescript-does-not-infer-the-environment-variables-type
Nov 23, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5bbc504
feat: enhance Vite configuration with tsconfig paths and update depen…
yamcodes 023cc7e
feat: enhance TypeScript type safety in Vite environment
yamcodes 6d5e4ff
feat: export Env schema for improved type safety in Vite configuration
yamcodes 4f52dc3
docs: add reference link to original ImportMetaEnv implementation
yamcodes 0d0d625
refactor: update ImportMetaEnv type definition for improved type safety
yamcodes 0703709
docs: refine comment on Env export suggestion in vite-env.d.ts
yamcodes 081545d
docs: update Vite plugin documentation and add new page for type-safe…
yamcodes 1eda22f
feat: add `ImportMetaEnvAugmented` type for typesafe `import.meta.env`
yamcodes 40d7795
docs: clarify comments in vite-env.d.ts and update documentation for …
yamcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@arkenv/vite-plugin": patch | ||
| --- | ||
|
|
||
| #### Add `ImportMetaEnvAugmented` type helper for typesafe `import.meta.env`. | ||
|
|
||
| Add a new `ImportMetaEnvAugmented` type that augments `import.meta.env` with your environment variable schema. This provides full type safety and autocomplete for all your `VITE_*` environment variables in client code. | ||
|
|
||
| Implementation inspired by [Julien-R44](https://github.com/Julien-R44)'s [vite-plugin-validate-env](https://github.com/Julien-R44/vite-plugin-validate-env#typing-importmetaenv). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,21 @@ | ||
| /// <reference types="vite/client" /> | ||
|
|
||
| // Import the Env schema type from vite.config.ts | ||
| // Note: In a real project, you might want to export Env from a separate env.ts file | ||
| // and import it like: import type { Env } from "./env"; | ||
| type ImportMetaEnvAugmented = | ||
| import("@arkenv/vite-plugin").ImportMetaEnvAugmented< | ||
| typeof import("../vite.config").Env | ||
| >; | ||
|
|
||
| interface ViteTypeOptions { | ||
| // By adding this line, you can make the type of ImportMetaEnv strict | ||
| // to disallow unknown keys. | ||
| // See: https://vite.dev/guide/env-and-mode#intellisense-for-typescript | ||
| // ⚠️ This option requires Vite 6.3.x or higher | ||
| strictImportMetaEnv: unknown; | ||
| } | ||
|
|
||
| // Now import.meta.env is totally typesafe and based on your `Env` schema definition | ||
| // Only VITE_* prefixed variables will be included (PORT is excluded) | ||
| interface ImportMetaEnv extends ImportMetaEnvAugmented {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| { | ||
| "files": [], | ||
| "compilerOptions": { | ||
| "baseUrl": ".", | ||
| "paths": { | ||
| "@/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "references": [ | ||
| { "path": "./tsconfig.app.json" }, | ||
| { "path": "./tsconfig.node.json" } | ||
| { | ||
| "path": "./tsconfig.app.json" | ||
| }, | ||
| { | ||
| "path": "./tsconfig.node.json" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
apps/www/content/docs/vite-plugin/typing-import-meta-env.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| title: Typing import.meta.env | ||
| --- | ||
|
|
||
| For most use cases, you'll want to access your environment variables in your client code. [In a Vite project, this is done via `import.meta.env`](https://vite.dev/guide/env-and-mode). | ||
|
|
||
| With ArkEnv, this can be typesafe with a one-time project setup: add a small file in your `src` directory and ensure your `Env` schema is defined in `vite.config.ts` (or exported from a separate file). | ||
|
|
||
| > [!NOTE] | ||
| > After you complete this setup, each time you add/remove environment variables from your schema, your `import.meta.env` will always be typesafe - no codegen needed. | ||
|
|
||
| ## Setup | ||
|
|
||
| Add this to a `vite-env.d.ts` file in your `src` directory: | ||
|
|
||
| ```ts title="src/vite-env.d.ts" | ||
| /// <reference types="vite/client" /> | ||
|
|
||
| type ImportMetaEnvAugmented = | ||
| import("@arkenv/vite-plugin").ImportMetaEnvAugmented< | ||
| typeof import("../vite.config").Env | ||
| >; | ||
|
|
||
| interface ViteTypeOptions { | ||
| // By adding this line, you can make the type of ImportMetaEnv strict | ||
| // to disallow unknown keys. | ||
| // See: https://vite.dev/guide/env-and-mode#intellisense-for-typescript | ||
| // ⚠️ This option requires Vite 6.3.x or higher | ||
| strictImportMetaEnv: unknown; | ||
| } | ||
|
|
||
| // Augment import.meta.env with your schema | ||
| // Only `VITE_*` prefixed variables will be included | ||
| interface ImportMetaEnv extends ImportMetaEnvAugmented {} | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Once set up, `import.meta.env` is fully typesafe: | ||
|
|
||
| ```ts title="src/App.tsx" | ||
| // TypeScript knows about your VITE_* variables | ||
| const apiUrl = import.meta.env.VITE_API_URL; // ✅ Type-safe | ||
| const port = import.meta.env.PORT; // ❌ Error: PORT is not in ImportMetaEnv | ||
|
|
||
| // Autocomplete works too! | ||
| import.meta.env.VITE_ // Shows all your VITE_* variables | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| The `ImportMetaEnvAugmented` type: | ||
|
|
||
| 1. Extracts the inferred type from your schema (the result of `type()` from arkenv) | ||
| 2. Filters to only include variables matching the Vite prefix (defaults to `"VITE_"`) | ||
| 3. Makes them available on `import.meta.env` with full type safety | ||
|
|
||
| Server-only variables (like `PORT`) are automatically excluded from the client bundle and won't appear in `import.meta.env`. | ||
|
|
||
| ## Example with separate env file | ||
|
|
||
| As your project grows, you might want to define your schema in a dedicated file for better organization: | ||
|
|
||
| ```ts title="src/env.ts" | ||
| import { type } from "arkenv"; | ||
|
|
||
| export const Env = type({ | ||
| PORT: "number.port", | ||
| VITE_API_URL: "string", | ||
| VITE_API_KEY: "string", | ||
| VITE_MY_NUMBER: type("string").pipe((str) => Number.parseInt(str, 10)), | ||
| VITE_MY_BOOLEAN: type("string").pipe((str) => str === "true"), | ||
| }); | ||
|
|
||
| export type Env = typeof Env.infer; | ||
| ``` | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import arkenvVitePlugin from "@arkenv/vite-plugin"; | ||
| import { defineConfig } from "vite"; | ||
| import { Env } from "./src/env"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [arkenvVitePlugin(Env)], | ||
| }); | ||
| ``` | ||
|
|
||
| ```ts title="src/vite-env.d.ts" | ||
| /// <reference types="vite/client" /> | ||
|
|
||
| import type { ImportMetaEnvAugmented } from "@arkenv/vite-plugin"; | ||
| import type { Env } from "./env"; | ||
|
|
||
| interface ViteTypeOptions { | ||
| strictImportMetaEnv: unknown; | ||
| } | ||
|
|
||
| interface ImportMetaEnv extends ImportMetaEnvAugmented<typeof Env> {} | ||
| ``` | ||
|
|
||
| ## Credit | ||
| This implementation is inspired by [vite-plugin-validate-env by Julien-R44](https://github.com/Julien-R44/vite-plugin-validate-env#typing-importmetaenv). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.