Skip to content
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

Better typing for $env/dynamic/* #6202

Closed
elliott-with-the-longest-name-on-github opened this issue Aug 23, 2022 · 0 comments · Fixed by #6413
Closed

Better typing for $env/dynamic/* #6202

elliott-with-the-longest-name-on-github opened this issue Aug 23, 2022 · 0 comments · Fixed by #6413
Labels
breaking change $env Bugs related to the `$env` family of modules low hanging fruit types / typescript
Milestone

Comments

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Describe the problem

When I put together the $env/dynamic/* modules, I made a stupid mistake thinking that we couldn't provide generated types similar to $env/static/*. This resulted in the dynamic modules being typed like so:

declare module '$env/dynamic/public' {
  let env: App.PublicEnv;
}
declare module '$env/dynamic/public' {
  let env: App.PrivateEnv;
}

This forces users to explicitly type their data using the App types, which is annoying -- it's "fake" type safety -- nothing is guaranteeing those keys even exist on your env object.

For some reason (maybe I was tired?) I didn't see the obvious solution...

Describe the proposed solution

declare module '$env/dynamic/public' {
  let env: {
    PUBLIC_FOO: string | undefined;
    PUBLIC_BAR: string | undefined;
    [key: string]: string;
  };
}
declare module '$env/dynamic/public' {
  let env: {
    PRIVATE_FOO: string | undefined;
    PRIVATE_BAR: string | undefined;
    [key: string]: string;
  };
}

This still allows for arbitrary optional dynamic variables (such as feature flags or environment-dependent settings), but removes the need to use "fake" types to provide autocomplete.

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

@benmccann benmccann added this to the 1.0 milestone Aug 24, 2022
@dummdidumm dummdidumm added the $env Bugs related to the `$env` family of modules label Aug 26, 2022
Rich-Harris added a commit that referenced this issue Aug 30, 2022
* use descriptions in generated env types

* generate dynamic env types

* remove references to PublicEnv and PrivateEnv

* generate dynamic env types - closes #6202
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change $env Bugs related to the `$env` family of modules low hanging fruit types / typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants