Skip to content

[Feature proposal] Use .d.ts as schema for type safety#38

Open
lhhong wants to merge 1 commit intokeithmorris:developfrom
lhhong:develop
Open

[Feature proposal] Use .d.ts as schema for type safety#38
lhhong wants to merge 1 commit intokeithmorris:developfrom
lhhong:develop

Conversation

@lhhong
Copy link

@lhhong lhhong commented May 8, 2020

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

  1. README not updated
  2. Union string types not supported, future additions could include that with checking
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant