Safety measures (statically, not runtime) for libraries that require environment variables?
Suppose I have a Deno library which expects certain environment variables to have been configured, otherwise a crash will happen. Does anyone know of a good way to somehow ensure at "compile-time" that whatever Deno application is using the library has configured these environment variables?
10 Replies
but env vars will be read at runtime?
@arshxcx yes, i know, it's a weird question, not sure why i'm even asking... i figured maybe i've missed something, maybe one could do some cool stuff using dotenv, maybe something involving weird type inference? i don't even know man.
is your dev environment the same as your deployment environment?
nah, not at all
so you only want safety for the dev env
well, i guess it's technically only possible for dev env... unless i'm missing something. right now, i use a zod schema which gets fed Deno.env.toObject(), so it will crash at startup. i'm just thinking maybe there's something more clever.
well yeah its only possible for dev env, the alternative would be a type system that can predict the future
if you want the put in the effort, you could have a script that periodically writes Deno.env to a .ts file and exports it as an object with
as const
you could dynamically import the file
and you would have the exact types
with exact valuesyeah, currently i'm experimenting with importing the environment configuration as a JSON import...
yeah json works if you dont need to look at the values, json imports just give you keys
ah, good point
i'll try your idea, thanks for your input