Skip to content

Commit 50082ae

Browse files
committed
fix: default worker entrypoint support with wrangler types
1 parent 08b37f8 commit 50082ae

File tree

1 file changed

+5
-3
lines changed
  • packages/wrangler/src/type-generation

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,13 @@ export async function generateEnvTypes(
464464
: undefined;
465465

466466
const exportExists = serviceEntry?.exports?.some(
467-
(e) => e === service.entrypoint
467+
(e) => e === (service.entrypoint ?? "default")
468468
);
469469

470470
let typeName: string;
471471

472472
if (importPath && exportExists) {
473-
typeName = `Service<import("${importPath}").${service.entrypoint ?? "default"}>`;
473+
typeName = `Service<MaybeWorkerEntrypoint<typeof import("${importPath}").${service.entrypoint ?? "default"}>>`;
474474
} else if (service.entrypoint) {
475475
typeName = `Service /* entrypoint ${service.entrypoint} from ${service.service} */`;
476476
} else {
@@ -709,6 +709,8 @@ function generateTypeStrings(
709709
let baseContent = "";
710710
let processEnv = "";
711711

712+
const MaybeWorkerEntrypointType = `\ttype MaybeWorkerEntrypoint<T> = T extends new (...args: any[]) => infer EntrypointClass\n\t\t? EntrypointClass extends Rpc.WorkerEntrypointBranded ? EntrypointClass : undefined\n\t\t: undefined;`;
713+
712714
if (formatType === "modules") {
713715
if (
714716
isProcessEnvPopulated(compatibilityDate, compatibilityFlags) &&
@@ -717,7 +719,7 @@ function generateTypeStrings(
717719
// StringifyValues ensures that json vars are correctly types as strings, not objects on process.env
718720
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}`;
719721
}
720-
baseContent = `declare namespace Cloudflare {\n\tinterface Env {${envTypeStructure.map((value) => `\n\t\t${value}`).join("")}\n\t}\n}\ninterface ${envInterface} extends Cloudflare.Env {}${processEnv}`;
722+
baseContent = `declare namespace Cloudflare {\n${MaybeWorkerEntrypointType}\n\n\tinterface Env {${envTypeStructure.map((value) => `\n\t\t${value}`).join("")}\n\t}\n}\ninterface ${envInterface} extends Cloudflare.Env {}${processEnv}`;
721723
} else {
722724
baseContent = `export {};\ndeclare global {\n${envTypeStructure.map((value) => `\tconst ${value}`).join("\n")}\n}`;
723725
}

0 commit comments

Comments
 (0)