-
Notifications
You must be signed in to change notification settings - Fork 1k
widen multi-env vars types in wrangler types and add --strict-vars option to the command
#5086
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6fbfa01
fix: widen multi-env `vars` types in `wrangler types`
dario-piotrowicz 10a8797
add `loose-vars` option to `wrangler types`
dario-piotrowicz ada4e7d
fixup! add `loose-vars` option to `wrangler types`
dario-piotrowicz c262fc5
Update .changeset/unlucky-timers-sort.md
dario-piotrowicz 2b1fd3e
remove unnecessary double `map`
dario-piotrowicz 52648c6
fix improve and simplify types generation logic
dario-piotrowicz 7e57589
Update .changeset/proud-forks-build.md
dario-piotrowicz 1d8fb94
Update .changeset/unlucky-timers-sort.md
dario-piotrowicz bf39a65
remove unnecessary line
dario-piotrowicz 8a6d0dd
clarify `--strict-vars` changeset
dario-piotrowicz 41e01a6
simplify logic and add comments
dario-piotrowicz 5405cd8
update `the` to `a`
dario-piotrowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| "wrangler": patch | ||
| --- | ||
|
|
||
| fix: widen multi-env `vars` types in `wrangler types` | ||
|
|
||
| Currently, the type generated for `vars` is a string literal consisting of the value of the variable in the top level environment. If multiple environments | ||
| are specified this wrongly restricts the type, since the variable could contain any of the values from each of the environments. | ||
|
|
||
| For example, given a `wrangler.toml` containing the following: | ||
|
|
||
| ``` | ||
| [vars] | ||
| MY_VAR = "dev value" | ||
|
|
||
| [env.production.vars] | ||
| MY_VAR = "prod value" | ||
| ``` | ||
|
|
||
| running `wrangler types` would generate: | ||
|
|
||
| ```ts | ||
| 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: | ||
|
|
||
| ```ts | ||
| interface Env { | ||
| MY_VAR: "dev value" | "prod value"; | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| add `--strict-vars` option to `wrangler types` | ||
|
|
||
| add a new `--strict-vars` option to `wrangler types` that developers can (by setting the | ||
| flag to `false`) use to disable the default strict/literal types generation for their variables | ||
|
|
||
| opting out of strict variables can be useful when developers change often their `vars` values, | ||
| even more so when multiple environments are involved | ||
|
|
||
| ## Example | ||
|
|
||
| With a toml containing: | ||
|
|
||
| ```toml | ||
| [vars] | ||
| MY_VARIABLE = "production_value" | ||
| MY_NUMBERS = [1, 2, 3] | ||
|
|
||
| [env.staging.vars] | ||
| MY_VARIABLE = "staging_value" | ||
| MY_NUMBERS = [7, 8, 9] | ||
| ``` | ||
|
|
||
| the `wrangler types` command would generate the following interface: | ||
|
|
||
| ``` | ||
| interface Env { | ||
| MY_VARIABLE: "production_value" | "staging_value"; | ||
| MY_NUMBERS: [1,2,3] | [7,8,9]; | ||
| } | ||
| ``` | ||
|
|
||
| while `wrangler types --strict-vars=false` would instead generate: | ||
|
|
||
| ``` | ||
| interface Env { | ||
| MY_VARIABLE: string; | ||
| MY_NUMBERS: number[]; | ||
| } | ||
| ``` | ||
|
|
||
| (allowing the developer to easily change their toml variables without the | ||
| risk of breaking typescript types) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.