-
-
Notifications
You must be signed in to change notification settings - Fork 644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect client RPC response inference for Cloudflare Workers RPC return types which stub Arrays #3811
Comments
Hi @a-type I think it's not an actual bug of |
Fair, this may be more of a feature request, or possibly could move to a discussion to provide guidance on how to handle this in user-land. From Cloudflare's perspective I believe the type extension is warranted; their RPC magic necessarily changes the usage of returned objects somewhat and that needs to be reflected in the typings. As for handling this as a user, perhaps I can try to remove |
Ok, after experimentation, I found that this runtime no-op wrapper cleans up the array type so that it's properly instantiated when using the type RemoveDisposable<T extends Disposable> = T extends infer U & Disposable
? U
: never;
function wrapServiceData<T extends Disposable>(data: T): RemoveDisposable<T> {
return data as unknown as RemoveDisposable<T>;
}
// usage
const data = await ctx.env.SOME_RPC.getThing();
return ctx.json(wrapServiceData(data)); It's a little inconvenient to have to wrap all values passed to This is a sufficient workaround to not warrant internal changes to Hono I think, but I do think it would do well to mention this in the Cloudflare Workers section of the docs! |
This issue has been marked as stale due to inactivity. |
Closing this issue due to inactivity. |
For posterity, use the above workaround. For consideration by Hono maintainers -- adding the |
Hi @a-type
Documenting it is good. It would be great if you could create a PR! |
What version of Hono are you using?
4.6.15
What runtime/platform is your app running on? (with version if possible)
Cloudflare Workers
What steps can reproduce the bug?
Unfortunately both products use "RPC" naming here, so I'll try to be clear about which is which.
As recommended in Cloudflare Workers RPC docs for Typescript, I am specifying my bindings types using the
Service<T>
wrapper type to generate proper 'stub' typings which enforce requirements related to chained RPC methods becoming async, for example.So far I have only encountered issues with return types which were
Array
s before wrapping withService
.What is the expected behavior?
After returning the
Array
-based data viactx.json
, Hono's RPC typings would correctly infer the response typing as a native Array on the client-side.What do you see instead?
When using this wrapper type, Hono RPC cannot correctly infer typings of responses for Arrays. Instead, it produces a messy object type on the other end:
Additional information
Before passing the response to
ctx.json
, inspecting the type which theService<T>
wrapper has created looks something like this:Disposable
is defined in@cloudflare/workers-types
as an empty interface, and within standard lib as an object with a key onSymbol.dispose
. Since this type is otherwise an Array, I wonder if merely this intersection is undermining Hono RPC's typings, and if that could be accounted for by greedily looking forArray
inheritance and forcing the RPC response type to an array in such cases?Workaround
Removing the
Service<T>
wrapper on my RPC service bindings resolves the client typing inference problem, but exposes me to mistakes on the server-side regarding Cloudflare's RPC usage. This is an acceptable tradeoff for me a the moment, but it would be nice to have both sides correctly typed.The text was updated successfully, but these errors were encountered: