workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🚀 Feature Request: `wrangler types` generates wider types for environment variables

Open mw10013 opened this issue 1 year ago • 3 comments

Describe the solution

wrangler types seems to generate literals for environment variables in wrangler.toml. A wider type like string may be more helpful.

[vars]
ENVIRONMENT = "development"

generates

interface Env {
  ENVIRONMENT: "development";
}

Typescript then complains when I try to compare ENVIRONMENT against, say, 'production'.

Screenshot 2024-02-23 at 3 22 04 PM

mw10013 avatar Feb 23 '24 23:02 mw10013

@mw10013 thanks for the issue 🙂

I've opened a PR to hopefully address this: https://github.com/cloudflare/workers-sdk/pull/5086

With my changes if you have your variable defined in multiple environments in your config file and run wrangler types instead of getting the single string literal you get a union of all the possible values, please have a look and let me know if it does solve the issue 🙂🙏


PS: you can try seeing if the PR's prerelease works for you by running:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-wrangler-5086 types

dario-piotrowicz avatar Feb 24 '24 18:02 dario-piotrowicz

@dario-piotrowicz : I tried the PR prerelease and it works perfectly for my use case. World class typing. Thanks!

mw10013 avatar Feb 25 '24 00:02 mw10013

@mw10013 great! :rocket: thanks for giving it a try! :smile:

dario-piotrowicz avatar Feb 25 '24 11:02 dario-piotrowicz