fix: methods returning Promise<RpcStub<T>> no longer produce stub-of-stub result types - #253
Merged
Conversation
…ult types The string-keyed `__RPC_TARGET_BRAND` (and the workers brands in capnweb-validate) leaked into the keyof-based Provider/ValidatedStub key mapping, so a stub of a branded target still matched `Stubable` structurally. A method declared as `Promise<RpcStub<T>>` therefore had its already-stubbed result stubified again, producing a broken stub-of-stub type: awaits yielded `RpcStub<RpcStub<T>>` (TS2322) and passing the result as a pipelined argument failed (TS2345). Excluding the brand keys from stub surfaces fixes the root cause; on top of that, `Result` now elides declared stub returns via `ElideStub`, so `Promise<RpcStub<T>>` returns produce exactly the same `RpcPromise<T>` as `Promise<T>` returns. Elision is the chosen policy for stubable payloads only: such stubs await back to a stub either way, so the two declarations are genuinely interchangeable. Plain-interface stubs (`RpcStub<PlainApi>`) and `RpcStub<any>` are deliberately NOT elided — their awaited type would change if they were. The workerd interop tests gain `<any>` casts where a userspace stub is handed to workers-types' native `RpcStub`: without the leaked brand, a userspace stub no longer statically matches workers-types' `Stubable` (runtime interop is unchanged).
Restoring `__RPC_TARGET_BRAND` in the Provider key mapping keeps userspace stubs structurally assignable to workers-types' `Stubable`, so `new RpcStub(userspaceStub)` from `cloudflare:workers` typechecks again without casts (as it did before the brand exclusion). The brand exclusion turns out to be unnecessary for the stub-of-stub fix: `Stubify`/`Result`/`ElideStub` all check `StubBase` before `Stubable`, so a stub matching `Stubable` is caught as a stub first and never re-stubified. That ordering is the same mechanism that already protects callable stubs, which genuinely match `Stubable` and cannot have that excluded. Known residual: an explicit `RpcPromise<RpcStub<T>>` annotation still produces the old double-stub shape (matching main); the changeset already directs users to write `RpcPromise<T>` instead.
🦋 Changeset detectedLatest commit: b86d6bb The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
|
@ndisidore Bonk workflow failed. Check the logs for details. View workflow run · To retry, trigger Bonk again. |
dimitropoulos
approved these changes
Aug 19, 2026
dimitropoulos
left a comment
Collaborator
There was a problem hiding this comment.
type level tests, woop woop
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.
Presently declaring an RPC method to return
Promise<RpcStub<T>>gave you a broken type: an internal brand property made the type machinery treat the already-stubbed result as something that still needed wrapping, so awaiting yieldedRpcStub<RpcStub<T>>(TS2322) and passing the result into another RPC call failed to compile (TS2345), even though the runtime behaved correctly.Now the type machinery recognizes existing stubs before re-wrapping, so a
Promise<RpcStub<T>>return types identically to aPromise<T>return and awaits, pipelined calls, and .map() over stub arrays all compile.Nothing changes at runtime; the one migration is rewriting any explicit
RpcPromise<RpcStub<T>>annotation asRpcPromise<T>.