t3-oss / t3-env

https://env.t3.gg
MIT License
2.67k stars 86 forks source link

How to augment `proccess.env` as well from `createEnv` output. #200

Closed AhmedBaset closed 7 months ago

AhmedBaset commented 7 months ago

Is there a way to augment NodeJS.ProccessEnv by extending the env object without rewriting them one by one.

I've tried:

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url(),
  },
  runtimeEnv: {
    DATABASE_URL: process.env.DATABASE_URL,
  },
});

export type Env = typeof env
// env.d.ts
import type {Env} from "@/env";

declare global {
  namespace NodeJS {
    interface ProcessEnv extends Env {}
  }
}

I get errors in:

juliusmarminge commented 7 months ago

i dont think this is possible, as the error suggests it creates a circular reference.

what's the use case why you wanna do this? I believe augmenting process.env here is kinda scary since we have no assurance if the type iss correct then considering transforms, default values etc aren't taken into account then.

e.g.:

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url().default("mysql://localhost:3306/db"),
  },
  runtimeEnv: {
    DATABASE_URL: process.env.DATABASE_URL,
  },
});

here env.DATABASE_URL will be guaranteed to be a string, but process.env.DATABASE_URL can be undefined. augmenting the type would potentailly cause a runtime error

AhmedBaset commented 7 months ago

Got it, thanks

It was a weird use case

going to close this