From 64b082eff78f59ef7cad5b4848d2aa8a21a6d8d6 Mon Sep 17 00:00:00 2001 From: Julien Date: Fri, 29 Nov 2024 11:14:13 +0100 Subject: [PATCH] docs: note about unknown variables Close #27 --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 !