Skip to content

Instantly share code, notes, and snippets.

@amanzrx4
Last active June 17, 2024 09:47
Show Gist options
  • Save amanzrx4/ac1ae8c3add05f3dc402abbee9eb5fe0 to your computer and use it in GitHub Desktop.
Save amanzrx4/ac1ae8c3add05f3dc402abbee9eb5fe0 to your computer and use it in GitHub Desktop.
type safe env variable parsing with valibot
import { config } from 'dotenv';
import type { InferInput, ValiError } from 'valibot';
import { object, parse, string } from 'valibot';
config();
const envVariables = object({
MY_KEY: string(),
});
parse(envVariables, process.env);
try {
parse(envVariables, process.env);
} catch (e) {
console.error((e as ValiError<typeof envVariables>).message);
process.exit(1);
}
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface ProcessEnv extends InferInput<typeof envVariables> {}
}
}
export {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment