gen-vite-env-types
v1.0.0
Published
Takes your `.env` file as input
Downloads
2
Readme
gen-vite-env-types
Takes your .env
file as input
SESSION_SECRET=asdjpfowqip
STRIPE_ACCESS_TOKEN=qoi120wqe
And generates a .d.ts
file
interface ImportMetaEnv extends Readonly<Record<string, string>> {
readonly VITE_SESSION_SECRET: string
readonly VITE_STRIPE_ACCESS_TOKEN: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
Now process.env.SESSION_SECRET
will autocomplete and be type-safe.
Customize
gen-vite-env-types
respects changes made to generated files, meaning you can overwrite .env.example
and env.d.ts
values, this can be helpful if you want a union type:
interface ImportMetaEnv extends Readonly<Record<string, string>> {
readonly NODE_ENV: 'development' | 'production'
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
```
Or if you want to persist `.env.example` values:
```sh
PORT=3000
Usage
npx gen-vite-env-types path/to/.env
Options
-V, --version Show version number
-h, --help Show usage information
-o, --types-output Output name/path for types file | defaults to `env.d.ts`
-e, --example-env-path Path to save .env.example file
-r, --rename-example-env Custom name for .env example output file | defaults to `env.example` if omitted
Examples with options
npx gen-vite-env-types .env -o src/types/env.d.ts -e .
# With custom example env file name
npx gen-vite-env-types .env -o src/types/env.d.ts -e . -r .env.test