I like the tricks :) – Kasia
Make your NodeJS env both type-safe and autocompletable in a few lines of code.
import { z } from "zod"
const EnvSchema = z.object({
LOG_LEVEL: z
.union([z.literal("debug"), z.literal("info")])
.default("info"),
LOG_FILE: z.string(),
})
declare global {
namespace NodeJS {
interface ProcessEnv extends z.infer<typeof EnvSchema> {}
}
}
process.env = { ...process.env, ...EnvSchema.parse(process.env) }
// ✅ Throws if LOG_FILE is missing
// ✅ Defaults LOG_LEVEL to "info" if missing
process.env.LOG_LEVEL
// ✅ ^ "debug" | "info"
// ✅ Autocomplete
process.env.LOG_FILE
// ✅ ^ string
// ✅ Autocomplete
Crafted with 💛 by Riccardo
Crafted with 💛 by Riccardo