fix(cloudflare/workers): type named entrypoint bindings with their RPC surface - #1415
Draft
Mkassabov wants to merge 1 commit into
Draft
fix(cloudflare/workers): type named entrypoint bindings with their RPC surface#1415Mkassabov wants to merge 1 commit into
Mkassabov wants to merge 1 commit into
Conversation
Contributor
|
Install the packages built from this commit: Alchemyalchemy bun add https://pkg.ing/alchemy/9c0075a@alchemy.run/better-auth bun add https://pkg.ing/@alchemy.run/better-auth/9c0075a@alchemy.run/cloudflare-runtime bun add https://pkg.ing/@alchemy.run/cloudflare-runtime/9c0075a@alchemy.run/frontend-frameworks bun add https://pkg.ing/@alchemy.run/frontend-frameworks/9c0075a@alchemy.run/node-utils bun add https://pkg.ing/@alchemy.run/node-utils/9c0075a@alchemy.run/pr-package bun add https://pkg.ing/@alchemy.run/pr-package/9c0075a@alchemy.run/floci bun add https://pkg.ing/@alchemy.run/floci/9c0075aDistilled@distilled.cloud/core bun add https://pkg.ing/@distilled.cloud/core/cb11c5b@distilled.cloud/aws bun add https://pkg.ing/@distilled.cloud/aws/cb11c5b@distilled.cloud/axiom bun add https://pkg.ing/@distilled.cloud/axiom/cb11c5b@distilled.cloud/cloudflare bun add https://pkg.ing/@distilled.cloud/cloudflare/cb11c5b@distilled.cloud/hetzner bun add https://pkg.ing/@distilled.cloud/hetzner/cb11c5b@distilled.cloud/neon bun add https://pkg.ing/@distilled.cloud/neon/cb11c5b@distilled.cloud/planetscale bun add https://pkg.ing/@distilled.cloud/planetscale/cb11c5b |
sam-goodwin
reviewed
Aug 31, 2026
| export const Worker = Cloudflare.Worker("EntrypointEnvTypeProbe", { | ||
| script: "export default {}", | ||
| env: { | ||
| API: Cloudflare.WorkerEntrypoint<typeof Api>(target, "Api"), |
Contributor
There was a problem hiding this comment.
Why not Cloudflare.WorkerEntrypoint<Api>(target, "Api")
sam-goodwin
reviewed
Aug 31, 2026
Comment on lines
+156
to
+185
| /** | ||
| * Runtime type of a `Cloudflare.WorkerEntrypoint(...)` env entry. | ||
| * | ||
| * `Entrypoint` is the class the binding names, supplied as an explicit type | ||
| * argument (`Cloudflare.WorkerEntrypoint<typeof Api>(target, "Api")`) — the | ||
| * entrypoint name is a string at the value level, so nothing links it to the | ||
| * target module's exports on its own. With it, the entry types as that | ||
| * class's RPC surface (`Service<typeof Api>`); without it, the binding falls | ||
| * back to the target Worker's own default-entrypoint type: an Effect-native | ||
| * Worker's `Rpc<Shape>` wire shape, otherwise a bare `Fetcher`. | ||
| */ | ||
| export type EntrypointStub<Entrypoint, Target> = [Entrypoint] extends [ | ||
| undefined, | ||
| ] | ||
| ? Target extends AlchemyRpc<infer Shape extends object> | ||
| ? RpcWireShape<Shape> & Service | ||
| : Fetcher | ||
| : Entrypoint extends AlchemyRpc<infer Shape extends object> | ||
| ? RpcWireShape<Shape> & Service | ||
| : Entrypoint extends abstract new (...args: any[]) => infer Instance | ||
| ? EntrypointStubOf<Instance> | ||
| : EntrypointStubOf<Entrypoint>; | ||
|
|
||
| /** {@link EntrypointStub} for an entrypoint *instance* type. */ | ||
| type EntrypointStubOf<Instance> = [Instance] extends [ | ||
| Rpc.WorkerEntrypointBranded, | ||
| ] | ||
| ? Service<Extract<Instance, Rpc.WorkerEntrypointBranded>> | ||
| : // A plain method-bag shape (no `cloudflare:workers` brand). | ||
| Fetcher & { [K in keyof Instance]: Instance[K] }; |
Contributor
There was a problem hiding this comment.
Did we not already have something for this?
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Cloudflare.WorkerEntrypoint(target, "Api")returned a non-genericWorkerEntrypointBinding, so no information about the entrypoint class reachedInferEnv. Its first branch mapped the binding to a bareFetcher(i.e.Fetcher<undefined>—fetch+connectonly), so calling an RPC method on it failed to compile:The entrypoint name is a runtime string, so nothing links
"Api"to the target module's exports on its own. The class is now passed as a type argument:WorkerEntrypointBindingcarries the class in a phantom field, andInferEnvmaps it through a newEntrypointStub:cloudflare:workers'WorkerEntrypoint(branded) →Service<Instance>Fetcher & { …methods }Rpc<Shape>worker →RpcWireShape<Shape> & ServiceFetcherand still compiletest/types/WorkerEntrypointEnv.tsis a compile-only probe:greetisPromise<string>, non-promise returns are promisified,fetchstill resolves, and the untyped form rejectsgreetunder@ts-expect-error.The docs claimed the old behavior worked ("
InferEnvtypes the binding as aFetcherservice stub — RPC methods are called on it directly"); the JSDoc and the Workers guide now teach the type-argument form.