Skip to content

Commit

Permalink
docs: note about unknown variables
Browse files Browse the repository at this point in the history
Close #27
  • Loading branch information
Julien-R44 committed Nov 29, 2024
1 parent d53be19 commit 64b082e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 !
Expand Down

0 comments on commit 64b082e

Please sign in to comment.