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

fix: widen multi-env `vars` types in `wrangler types`

Open dario-piotrowicz opened this issue 1 year ago • 5 comments

Fixes #5082

What this PR solves / how to test:

Currently types for variable generate string literal, those are appropriate when a single environment has been specified in the config file but if multiple environments are specified this however wrongly restricts the typing, the changes here fix such incorrect behavior.

For example, given a wrangler.toml containing the following:

[vars]
MY_VAR = "dev value"
[env.production]
[env.production.vars]
MY_VAR = "prod value"

running wrangler types would generate:

interface Env {
	MY_VAR: "dev value";
}

making typescript incorrectly assume that MY_VAR is always going to be "dev value"

after these changes, the generated interface would instead be:

interface Env {
	MY_VAR: "dev value" | "prod value";
}

Author has addressed the following:

  • Tests
    • [x] Included
    • [ ] Not necessary because:
  • Changeset (Changeset guidelines)
    • [x] Included
    • [ ] Not necessary because:
  • Associated docs
    • [ ] Issue(s)/PR(s):
    • [x] Not necessary because: this is a quality of life improvement, not requiring documentation

Note for PR author:

We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label highlight pr review so future reviewers can take inspiration and learn from it.

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

🦋 Changeset detected

Latest commit: df55b66a39a9055dc3acf57cb6f407432ca75719

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Feb 24 '24 17:02 changeset-bot[bot]

A wrangler prerelease is available for testing. You can install this latest build in your project with:

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

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/5086/npm-package-wrangler-5086

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-wrangler-5086 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-create-cloudflare-5086 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-cloudflare-kv-asset-handler-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-miniflare-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-cloudflare-pages-shared-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/8032131415/npm-package-cloudflare-vitest-pool-workers-5086

Note that these links will no longer work once the GitHub Actions artifact expires.


[email protected] includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240208.0
workerd 1.20240208.0 1.20240208.0
workerd --version 1.20240208.0 2024-02-08

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

github-actions[bot] avatar Feb 24 '24 17:02 github-actions[bot]

Codecov Report

Attention: Patch coverage is 97.36842% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 70.42%. Comparing base (cab7e1c) to head (df55b66).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5086      +/-   ##
==========================================
+ Coverage   70.33%   70.42%   +0.08%     
==========================================
  Files         298      298              
  Lines       15515    15536      +21     
  Branches     3987     3990       +3     
==========================================
+ Hits        10913    10941      +28     
+ Misses       4602     4595       -7     
Files Coverage Δ
packages/wrangler/src/type-generation.ts 99.29% <100.00%> (+0.09%) :arrow_up:
packages/wrangler/src/config/index.ts 90.44% <91.66%> (+0.27%) :arrow_up:

... and 6 files with indirect coverage changes

codecov[bot] avatar Feb 24 '24 17:02 codecov[bot]

I've converted this PR to draft as it's been decided by the team that we should go with generic string, etc... types instead of union of string literals etc...

dario-piotrowicz avatar Feb 26 '24 17:02 dario-piotrowicz

Hi :wave:,

Thanks @dario-piotrowicz for the PR, this is something we had to fix manually after running wrangler types. I believe your implementation would improve correctness, and I was looking forward to seeing it land. Widening the type to string is a weird change in my opinion - updating a vars variable is no different to adding/deleting a KV namespace, it causes the same desync between the config and the type declaration file. Some frameworks auto-generate their equivalent of wrangler types (eg. astro sync, nuxi prepare) before building or starting a dev server, although I admit that's a slightly different case as those files would generally be in .gitignore. Still, I don't think a union would cause any more harm than string.

DaniFoldi avatar Feb 26 '24 18:02 DaniFoldi

I'd be keen to know what the reasoning was for using a generic string type instead of string literals. Surely having more robust types would benefit those of us using typescript? My solution atm is to override the type.

jasperdunn avatar Jul 21 '24 06:07 jasperdunn