Skip to content

Instantly share code, notes, and snippets.

@MarwanShehata
Last active April 23, 2025 19:46
Show Gist options
  • Select an option

  • Save MarwanShehata/6bca13ceed615e713f534c7bdb8025f3 to your computer and use it in GitHub Desktop.

Select an option

Save MarwanShehata/6bca13ceed615e713f534c7bdb8025f3 to your computer and use it in GitHub Desktop.
This TypeScript code enhances environment variable type safety through Zod validation. It creates a schema that explicitly defines expected environment variables. The code then validates the current environment against this schema and extends TypeScr
/* eslint-disable @typescript-eslint/no-namespace */
/**
* Configuration for type-safe environment variables.
* Imported through src/app/page.tsx
* @see https://x.com/mattpocockuk/status/1760991147793449396
*/
import { z } from 'zod'
const envVariables = z.object({
NEXT_PUBLIC_SHOW_LOGGER: z.enum(['true', 'false']).optional(),
DATABASE_URL: z.string().optional(),
})
envVariables.parse(process.env)
declare global {
namespace NodeJS {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface ProcessEnv extends z.infer<typeof envVariables> {}
}
}
export const isProd = process.env.NODE_ENV === 'production'
export const isLocal = process.env.NODE_ENV === 'development'
export const showLogger =
isLocal || process.env.NEXT_PUBLIC_SHOW_LOGGER === 'true'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment