fix(types): resolve RPC type inference for arrow functions returning Promise chains - #5251
fix(types): resolve RPC type inference for arrow functions returning Promise chains#5251yigiterturk-dev wants to merge 1 commit into
Conversation
Fix MergeTypedResponse to recursively unwrap nested Promise types, allowing arrow functions that return Promise chains (e.g., Promise.resolve(...).then((d) => c.json(d))) to correctly infer TypedResponse output types in the RPC client. Previously, handlers returning Promise<Response & TypedResponse<T>> would result in 'unknown' output types on the client side because MergeTypedResponse did not handle nested Promise unwrapping. The fix adds recursive Promise unwrapping to MergeTypedResponse while preserving all existing type behavior. Closes #4765
|
Closing this in favour of documenting the limitation. While working on the fix I found that the response type is not lost in So this is the case @yusukebe described in #4765 — fixing it in the type definitions is not a small change. Following the suggestion there to document it instead: honojs/website#897. |
Summary
Fix
MergeTypedResponseto correctly inferTypedResponseoutput types when handlers return Promise chains (e.g.,Promise.resolve(...).then((d) => c.json(d))).Closes #4765
Problem
When using arrow functions that return a Promise chain directly, the RPC client infers
unknownas the output type:Root Cause
TypeScript infers the handler return type as
Promise<Response & TypedResponse<{hello: string}, 200, "json">>. The previousMergeTypedResponseimplementation only unwrapped one level ofPromise, so when the inner type was still wrapped in a Promise (nested Promise chains), it fell through to the defaultTypedResponse(with no type parameter), resulting inunknownon the client side.Solution
Added recursive Promise unwrapping to
MergeTypedResponse:Key changes:
TypedResponseor fall through to the default.Promise<void>case — The existing fallback toTypedResponsealready handles this correctly; the special case was redundant.Testing
src/client/client.test.tsthat verifies the exact scenario from BUG: RPC types broken when using arrow function with a Promise #4765