This proposal adds type safety for typescript by providing the ability to use type declarations directly as a schema. With schema checking, we can take advantage of the fact that this library ensures the respective keys in process.env will be present.
Existing solutions are not as great. We could litter the typescript code with if KEY !== undefined or with !! operator but risk having typographical errors. Having process.env type declaration facilitates code completion. While we can use a type declaration separate from the schema, being able to have one single source of truth avoids the need to change multiple places.
Notes
README not updated
Union string types not supported, future additions could include that with checking
I have also considered declaration code generation but it can be painful for a small project without proper build pipelines
Breaks: type declaration of IEnvironmentMap changed to { [name: string]: string | undefined }. I believe dotenv's type declaration of { [name: string]: string } is a mistake. With this change, it no longer makes sense to carry this mistake down.
This proposal adds type safety for typescript by providing the ability to use type declarations directly as a schema. With schema checking, we can take advantage of the fact that this library ensures the respective keys in
process.env
will be present.Existing solutions are not as great. We could litter the typescript code with
if KEY !== undefined
or with!!
operator but risk having typographical errors. Havingprocess.env
type declaration facilitates code completion. While we can use a type declaration separate from the schema, being able to have one single source of truth avoids the need to change multiple places.Notes
Breaks: type declaration of IEnvironmentMap changed to
{ [name: string]: string | undefined }
. I believedotenv
's type declaration of{ [name: string]: string }
is a mistake. With this change, it no longer makes sense to carry this mistake down.