Skip to content

Instantly share code, notes, and snippets.

@meiazero
Created April 14, 2024 15:40
Show Gist options
  • Save meiazero/ffad53ef6b3f3638a1a1715ff25e084b to your computer and use it in GitHub Desktop.
Save meiazero/ffad53ef6b3f3638a1a1715ff25e084b to your computer and use it in GitHub Desktop.
Trick to extend node.js native `ProcessEnv` interface to include your own environment variables with zod
import { z } from "zod";
export const envVariables = z.object({
// your env variables like:
HOST: z.string(),
PORT: z.number().int().positive(),
});
// Parse the environment variables to ensure they match the schema
envVariables.parse(process.env);
// Extend the ProcessEnv interface with the inferred type from envVariables
declare global {
namespace NodeJS {
interface ProcessEnv extends z.infer<typeof envVariables> {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment