Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Created May 6, 2024 23:46
Show Gist options
  • Save fzn0x/cedc9ff2e6c99e1c4a20f4a93eb98a19 to your computer and use it in GitHub Desktop.
Save fzn0x/cedc9ff2e6c99e1c4a20f4a93eb98a19 to your computer and use it in GitHub Desktop.
wip: dynamic handler types to enforce return value in Hono handlers
// Handler type when `next` is NOT provided and a response is expected
export type HandlerWithoutNext<E extends Env = any, P extends string = any, I extends Input = BlankInput, R extends HandlerResponse<any> = any> =
(c: Context<E, P, I>) => R;
// Handler type when `next` is provided
export type HandlerWithNext<E extends Env = any, P extends string = any, I extends Input = BlankInput, R extends HandlerResponse<any> = any> =
(c: Context<E, P, I>, next: Next) => void;
// Union type to combine both handler types
export type Handler<E extends Env = any, P extends string = any, I extends Input = BlankInput, R extends HandlerResponse<any> = any> = HandlerWithoutNext<E, P, I, R> | HandlerWithNext<E, P, I, R>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment