Signature
Parameters
The name of the environment variable to read
An array of allowed string values. Use
as const for type safety.Optional fallback value (must be one of the allowed values). If not provided and the variable is missing, an error will be thrown.
Returns
One of the allowed string values from the enum
Behavior
- Reads the environment variable and trims whitespace
- Falls back to the provided fallback value if the variable is not set
- Throws an error if both the variable and fallback are missing
- Validates that the value is one of the allowed enum values
- Throws a
TypeErrorif the value is not in the allowed list
Error Handling
- Throws
Errorwith message"$KEY is missing"when the environment variable is not set and no fallback is provided - Throws
TypeErrorwith message"$KEY is not one of value1, value2, value3: ACTUAL_VALUE"when the value is not in the allowed list
