diff --git a/README.md b/README.md index f65147a..5a4351a 100644 --- a/README.md +++ b/README.md @@ -263,9 +263,23 @@ interface ImportMetaEnv extends ImportMetaEnvAugmented { // Now import.meta.env is totally type-safe and based on your `env.ts` schema definition // You can also add custom variables that are not defined in your schema } +``` + +## Forbid unknown variables + +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. + +To enforce stricter typing and prevent the use of unknown variables, you can set up the following: +```ts +// lib/env.ts or wherever you want +import { ImportMetaEnvAugmented } from '@julr/vite-plugin-validate-env'; + +export const env: ImportMetaEnvAugmented = import.meta.env; ``` +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. + ## Sponsors If you like this project, [please consider supporting it by sponsoring it](https://github.com/sponsors/Julien-R44/). It will help a lot to maintain and improve it. Thanks a lot !