We all got used to the great DX of typescript, but when it comes to environment variables, we are back to the old days of javascript. We have to check if the variable is defined, if it is a string, if it is a number, etc. This is where zod comes in. Zod is a great library that allows us to define a schema and then use it to validate any input. Let’s see how we can use it. It provides both a runtime and a compile time validation on top of great type inference.
Define a schema
First, we need to define a schema. We can do it by using the z.object
function. It takes an object with the keys and the types of the values.
Create type helpers
We can use the infer
keyword to create a type helper that will infer the types of the schema.
Create a getter function
Now we get to the fun part. We can create a function that will take a key and return the value of the environment variable. We are going to use the infered type to make sure that the key is a valid key of the schema.
Few things to note here:
- We are using the
parse
function of the schema to parse the environment variables. This will throw an error if the environment variables are not valid. - We are using the
ENV[K]
type to make sure that the return type is the same as the type of the schema. - We are using the
EnvVars
type to make sure that the key is a valid key of the schema.
Add a default value
We can even turn it up a notch and add a default value. We can use the NonNullable
type to make sure that the default value is not null or undefined.
Usage
Conclusion
We can now use the environment variables with the same great DX of typescript. We get the runtime safety and the great DX ergonomics of typescript for our environemt variables.