Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AhmedBaset opened this issue Feb 23, 2024 · 2 comments
Closed

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

AhmedBaset opened this issue Feb 23, 2024 · 2 comments

Comments

@AhmedBaset
Copy link

AhmedBaset commented Feb 23, 2024

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:

  • Env: Type alias 'Env' circularly references itself.ts(2456)
  • env: 'env' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022);
@juliusmarminge
Copy link
Member

juliusmarminge commented Feb 29, 2024

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
Copy link
Author

Got it, thanks

It was a weird use case

going to close this

@AhmedBaset AhmedBaset closed this as not planned Won't fix, can't repro, duplicate, stale Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants