Inferring the response type of a handler
I'm trying to infer the return type of a route on my server. Any tips on what I'm doing wrong?
The route response type is
Foo is
any tips on what I'm doing wrong? I'm trying to get back something along the lines of {
message: string;
}
7 Replies
I think you need to use the infertype directly on the handler from the examples I've seen previously I think its
type foo = InferResponseType<typeof hellworld[0], 200>
thanks!
also gives
as does
InferResponseType
is meant to be used with the RPC client, not the endpoints themselves
i don't know if there's a good way to achieve what you're asking without getting into some gnarly typing
@dan—1106 what's your goal here?I'm using Hono as the backend for a Stripe App I'm developing. Because of the way that Stripe Apps are sandboxed and I need to handle custom headers I don't think I can use the RPC client on the front end. I tried to use the
hc
object just for typing, but it returns typed as any
Sounds like getting this to work might be a more fruitful approach?depends on what your goals and priorities are
there are a variety of ways to keep a client in sync with a handler's return type
your app type probably isn't making it over the monorepo boundary because it's not generated. try emitting types w/o transpiling (i assume you're using a CF worker), and use those
but i would probably statically type the handler and share that with the client instead
* though this won't work so well with
createHandlers
. why are you using that?I saw the comment in the docs that the factory helpers facilitate setting the proper TypeScript types, and given that was part of my goals I was incorporating them! When should
createHandlers
be used?tbh, i haven't come across a really compelling use-case
maybe if you wanted to package a series of middleware to make them more portable?
using the factory helpers can make it easier to share types, but it's sort of niche. there are usually simpler ways to do that, depending on your use-case