-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Describe the solution
Problem
Wrangler generates the exact type for environment variables defined. For example, the following wrangler.toml:
[vars]
AUTH_DOMAIN = "https://www.example.com"Generates this type:
interface Env {
AUTH_DOMAIN: "https://www.example.com";
}It should be assumed that environment variables are able to change, otherwise they would just be defined as constants instead. This kind of type generation seems to make more sense:
interface Env {
- AUTH_DOMAIN: "https://www.example.com";
+ AUTH_DOMAIN: string;
}I don't think it's correct to assume that the values defined in wrangler.toml are the only possible values for the environment variables.
Secrets
Another issue is that secrets are only defined inside .dev.vars which isn't checked into version control. This creates issues for CI where the type generation isn't idemponent because .dev.vars doesn't exist in the CI environment. Changing the type generation to be more general will allow placeholder values to be defined in wrangler.toml for types.
eg.
[vars]
AUTH_DOMAIN = "https://www.example.com"
AUTH_SECRET = "<secret>"interface Env {
AUTH_DOMAIN: string;
AUTH_SECRET: string;
}Different environments
This would also solve this relevant issue: #5850
Suggestion
Reference:
workers-sdk/packages/wrangler/src/type-generation.ts
Lines 157 to 173 in 5462ead
| export function constructType( | |
| key: string, | |
| value: string | number | boolean, | |
| useRawVal = true | |
| ) { | |
| const typeKey = constructTypeKey(key); | |
| if (typeof value === "string") { | |
| if (useRawVal) { | |
| return `${typeKey}: ${value};`; | |
| } | |
| return `${typeKey}: ${JSON.stringify(value)};`; | |
| } | |
| if (typeof value === "number" || typeof value === "boolean") { | |
| return `${typeKey}: ${value};`; | |
| } | |
| return `${typeKey}: unknown;`; | |
| } |
It makes more sense to me for the implementation of constructTypes to be something more like this
export function constructType(
key: string,
value: string | number | boolean,
useRawVal = true
) {
const typeKey = constructTypeKey(key);
- if (typeof value === "string") {
- if (useRawVal) {
- return `${typeKey}: ${value};`;
- }
- return `${typeKey}: ${JSON.stringify(value)};`;
- }
- if (typeof value === "number" || typeof value === "boolean") {
- return `${typeKey}: ${value};`;
- }
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
+ if (useRawVal) {
+ return `${typeKey}: ${value};`;
+ }
+ return `${typeKey}: ${typeof value};`;
+ }
return `${typeKey}: unknown;`;
}Changing the implementation or adding an extra flag to wrangler types to enable this behavior would be greatly appreciated.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status