Last active
April 23, 2025 19:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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