Something went wrong. Try again.
A simple JavaScript function and accompanying TypeScript types to validate your environemt variables.
Something went wrong. Try again.
TypeScript 100%
README.md
typed-validate-env-vars #
This is a single JavaScript function and a handful of TypeScript types that you can paste into your project to validate your environment variables at runtime.
Features #
- Extremely lightweight
- Super simple API
- Strict inferred types based on your input
- IDE hints and completion based on your input
- Variables are type-checked at runtime
- Clear error messages
- Zero dependencies
Usage #
const {
HOST, // typed as string
PORT, // typed as number
DO_LOGGING, // typed as boolean
INVALID_VAR // TypeScript error in your IDE
} = validateEnvVars(
'HOST', // string env vars are simple (the default)
['PORT', 'number'], // if you want a number or a boolean
['DO_LOGGING', 'boolean'] // just make a tuple and add the type
);
Why not a package? #
I'm not ruling out distributing this as a package in the future, but there are already plenty such packages. When I found that none of those met my needs, I made this.
In this age of supply-chain attacks happening all the time, I'd recommend you just copy the function and its types into your project. It has no runtime dependencies, so it really is that simple.
License #
I don't care, do whatever the hell you want.