Function Signature
Parameters
The name of the environment variable to read
An array of allowed string values. Use
as const for proper type inference.Returns
T[number] | undefined - One of the allowed enum values, or undefined if the variable is not set or empty.
Behavior
Returnsundefined when:
- The environment variable is not set
- The environment variable is an empty string
- The environment variable contains only whitespace
TypeError when:
- The value is not one of the allowed values
Example
When to Use
UsemaybeEnvEnum instead of envEnum when:
- The environment variable is optional
- You want to handle missing values gracefully with
undefined - You have a default behavior when the variable is not set
envEnum when:
- The environment variable is required for your application
- You want the application to fail fast if the variable is missing
