New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dotenv): add the expectVars
option for typing of return value of load()
#4447
base: main
Are you sure you want to change the base?
feat(dotenv): add the expectVars
option for typing of return value of load()
#4447
Conversation
…lue of `load()`, `loadSync()`
I'm not sure these checks need to be done as part of const vars = await load({
envPath: ".env",
defaultsPath: ".env.defaults",
});
assertExists(vars["SERVER"]);
assertExists(vars["PORT"]); |
This is the feature that I would definitely use, to be honest. This is the reference to one of the original requests, before I do think that this goes along with idea of automatizing of unpacking of The original idea was to get strong typing, and the only way to get strong typing is by listing the variables that you expect to have. And then if you do get the list, why not check that there are indeed there? While you can use strong typing (via |
This overlaps with the purpose of |
Well, the main purpose is the strong typing. The additional check of the existence of variables may be considered as an overlap (although, again the presence of the "example" file does not guarrantee the existence if the "example" file itself is out of date). If you do think that these checks are indeed duplicating the purpose of the "example" file, I can remove them But again — the original and main purpose is to provide the typing of the return value, which should help avoid typos in variable names. It does seem natural to me that Let me know… |
This kind of type narrowing can be done in a more generic way like zod or valibot. For example, the below example gives a nice typing to import z from "npm:zod";
import { load } from "jsr:@std/dotenv@^0";
const Options = z.object({
HOST: z.string(),
PORT: z.coerce.number(),
});
const options = Options.parse(await load()); I think we should rather explore these kinds of solution (or just recommend zod, valibot, etc because these tools tend to have to have an opinionated design) |
The requested feature that enables you to type the return value of
load()
If set,
expectVars
does two things:Record<"SERVER" | "PORT", string>
The check is performed regardless of whether the example file has been provided or not in the options — this is for simplicity of the algorithm, and among other things, it also confirms that the example file is up to date with the code.