Skip to content

Commit eefbf92

Browse files
committed
fix: wrangler types decouple env-interface from namespace
1 parent 59534ba commit eefbf92

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

.changeset/fine-years-create.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Wrangler types decouple env-interface from namespace.
6+
7+
The `wrangler types` command now adds a `BaseEnv` interface which is then extended by `Cloudflare.Env` and the `--env-interface`

packages/wrangler/e2e/types.test.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("types", () => {
4949
await helper.seed(seed);
5050
const output = await helper.run(`wrangler types`);
5151
expect(output.stdout).toContain("Generating project types...");
52-
expect(output.stdout).toContain("interface Env {");
52+
expect(output.stdout).toContain("interface Env extends BaseEnv {");
5353
expect(output.stdout).toContain("Generating runtime types...");
5454
expect(output.stdout).toContain("Runtime types generated.");
5555
expect(output.stdout).toContain(
@@ -74,16 +74,17 @@ describe("types", () => {
7474
);
7575
expect(file).toMatchInlineSnapshot(`
7676
"/* eslint-disable */
77-
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 5c82572f95137bbfb775d02fdf441070)
77+
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: fbdd9c78deb72b14fc06ea86f1940c82)
78+
interface BaseEnv {
79+
MY_VAR: "my-var-value";
80+
}
7881
declare namespace Cloudflare {
7982
interface GlobalProps {
8083
mainModule: typeof import("./src/index");
8184
}
82-
interface Env {
83-
MY_VAR: "my-var-value";
84-
}
85+
interface Env extends BaseEnv {}
8586
}
86-
interface Env extends Cloudflare.Env {}
87+
interface Env extends BaseEnv {}
8788
"
8889
`);
8990
});
@@ -99,7 +100,7 @@ describe("types", () => {
99100
).split("\n");
100101

101102
expect(lines[1]).toMatchInlineSnapshot(
102-
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: 5c82572f95137bbfb775d02fdf441070)"`
103+
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: fbdd9c78deb72b14fc06ea86f1940c82)"`
103104
);
104105
expect(lines[2]).match(
105106
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
@@ -126,7 +127,7 @@ describe("types", () => {
126127
).split("\n");
127128

128129
expect(lines[1]).toMatchInlineSnapshot(
129-
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: e981fccb455c04c58ced00f3442b83ac)"`
130+
`"// Generated by Wrangler by running \`wrangler types -c wranglerA.toml --env-interface MyCloudflareEnv ./cflare-env.d.ts\` (hash: ae7e85ef5d4b4e21c6e897e59e7c34fc)"`
130131
);
131132
expect(lines[2]).match(
132133
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/
@@ -158,7 +159,7 @@ describe("types", () => {
158159
const file2 = readFileSync(typesPath, "utf8");
159160

160161
// regenerates env types
161-
expect(file2).toContain("interface Env {");
162+
expect(file2).toContain("interface Env extends BaseEnv {");
162163
// uses cached runtime types
163164
expect(file2).toContain("// Begin runtime types");
164165
expect(file2).toContain("FAKE RUNTIME");
@@ -180,17 +181,18 @@ describe("types", () => {
180181
);
181182
expect(file).toMatchInlineSnapshot(`
182183
"/* eslint-disable */
183-
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 1457dc49fa39d3fb92583c9c66b2dbe5)
184+
// Generated by Wrangler by running \`wrangler types --include-runtime=false\` (hash: 33f3189d3ee367953f0e81f02a1f1800)
185+
interface BaseEnv {
186+
MY_VAR: "my-var-value";
187+
MY_SECRET_VAR: string;
188+
}
184189
declare namespace Cloudflare {
185190
interface GlobalProps {
186191
mainModule: typeof import("./src/index");
187192
}
188-
interface Env {
189-
MY_VAR: "my-var-value";
190-
MY_SECRET_VAR: string;
191-
}
193+
interface Env extends BaseEnv {}
192194
}
193-
interface Env extends Cloudflare.Env {}
195+
interface Env extends BaseEnv {}
194196
"
195197
`);
196198
});

packages/wrangler/src/type-generation/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ function generateTypeStrings(
781781
// StringifyValues ensures that json vars are correctly types as strings, not objects on process.env
782782
processEnv = `\ntype StringifyValues<EnvType extends Record<string, unknown>> = {\n\t[Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string;\n};\ndeclare namespace NodeJS {\n\tinterface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, ${stringKeys.map((k) => `"${k}"`).join(" | ")}>> {}\n}`;
783783
}
784-
baseContent = `declare namespace Cloudflare {${entrypointModule ? `\n\tinterface GlobalProps {\n\t\tmainModule: typeof import("${entrypointModule}");${configuredDurableObjects.length > 0 ? `\n\t\tdurableNamespaces: ${configuredDurableObjects.map((d) => `"${d}"`).join(" | ")};` : ""}\n\t}` : ""}\n\tinterface Env {${envTypeStructure.map((value) => `\n\t\t${value}`).join("")}\n\t}\n}\ninterface ${envInterface} extends Cloudflare.Env {}${processEnv}`;
784+
baseContent = `interface BaseEnv {${envTypeStructure.map((value) => `\n\t${value}`).join("")}\n}\ndeclare namespace Cloudflare {${entrypointModule ? `\n\tinterface GlobalProps {\n\t\tmainModule: typeof import("${entrypointModule}");${configuredDurableObjects.length > 0 ? `\n\t\tdurableNamespaces: ${configuredDurableObjects.map((d) => `"${d}"`).join(" | ")};` : ""}\n\t}` : ""}\n\tinterface Env extends BaseEnv {}\n}\ninterface ${envInterface} extends BaseEnv {}${processEnv}`;
785785
} else {
786786
baseContent = `export {};\ndeclare global {\n${envTypeStructure.map((value) => `\tconst ${value}`).join("\n")}\n}`;
787787
}

0 commit comments

Comments
 (0)