Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Typing environment variables #1143

Description

@pfried

As you have this nice fromEnv Helper:

export const fromEnv =

I thought you may like the idea of typing environment variables as well (following the snake uppercase convention for env params)

export type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}`
  ? `${T extends Capitalize<T> ? "_" : ""}${Lowercase<T>}${CamelToSnakeCase<U>}`
  : S;

export type EnvParameters<Type extends Record<string, string>> = Record<
  `${Uppercase<CamelToSnakeCase<Extract<keyof Type, string>>>}`,
  string
>;

Example:

export type WebhookEnvironmentVariables = {
  webhookSecret: string;
  eventQueueName: string;
};

const envParams: EnvParameters<WebhookEnvironmentVariables>

will result in a type

{
   WEBHOOK_SECRET: string;
   EVENT_QUEUE_NAME: string;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions