Skip to content

Commit db7de67

Browse files
authored
docs: recommend strictImportMetaEnv
1 parent cfa9502 commit db7de67

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pnpm add -D @julr/vite-plugin-validate-env
2828

2929
You can use the plugin with the [built-in validator](https://github.com/poppinss/validator-lite) for simple use cases, or with libraries like Zod for more advanced schemas.
3030

31-
> [!TIP]
31+
> [!TIP]
3232
> I would recommend using a dedicated `env.ts` file to keep your Vite config clean and separate from your environment variable definitions. See the [Using a Dedicated `env.ts` Config File](#using-a-dedicated-envts-config-file) section for more details.
3333
3434
### Built-in Validator
@@ -160,27 +160,19 @@ type ImportMetaEnvAugmented = import('@julr/vite-plugin-validate-env').ImportMet
160160
typeof import('../env').default
161161
>
162162

163+
interface ViteTypeOptions {
164+
// Avoid adding an index type to `ImportMetaDev` so
165+
// there's an error when accessing unknown properties.
166+
// ⚠️ This option requires Vite 6.3.x or higher
167+
strictImportMetaEnv: unknown
168+
}
169+
163170
interface ImportMetaEnv extends ImportMetaEnvAugmented {
164171
// Now import.meta.env is totally type-safe and based on your `env.ts` schema definition
165172
// You can also add custom variables that are not defined in your schema
166173
}
167174
```
168175

169-
## Forbid unknown variables
170-
171-
Since we rely on module augmentation to type `import.meta.env`, using unknown variables won’t trigger errors because the `ImportMetaEnv` interface from Vite includes a `[key: string]: string` signature.
172-
173-
To enforce stricter typing and prevent the use of unknown variables, you can set up the following:
174-
175-
```ts
176-
// lib/env.ts or wherever you want
177-
import { ImportMetaEnvAugmented } from '@julr/vite-plugin-validate-env';
178-
179-
export const env: ImportMetaEnvAugmented = import.meta.env;
180-
```
181-
182-
By using `env` instead of `import.meta.env` in your code, TypeScript will now throw an error if you try to access an unknown variable.
183-
184176
## 💖 Sponsors
185177

186178
If you find this useful, consider [sponsoring me](https://github.com/sponsors/Julien-R44)! It helps support and maintain the project 🙏

0 commit comments

Comments
 (0)