We decided to develop a utility function for asserting environment variables. Our project requires certain local environment variables that are not needed when the application is deployed. To simplify the assertions throughout different files, we wanted to create this utility. We aim to provide either:
- an array of keys required in both local and live environments; or
- an object containing local and live specific environment variables along with the 'isLocal' value.
If any of the required values are missing, an error will be thrown (assertion part).
Once all environment variables are present, the function should return an object with all the values for either the local or live environment as properties. We also want to utilize the same 'isLocal' variable passed and have TypeScript infer the return type from the assertion method based on it.
We encountered an issue regarding the ReturnType. We attempted the following logic:
IEnvConfig<LOCAL, LIVE>['isLocal'] extends true
? Record<LOCAL[number], string>
: Record<LIVE[number], string>
However, this condition always evaluates to false. Why do you think this happens?
This is our current implementation: Link to Sandbox code