Skip to content

fix(types): accept async route hooks in shorthand options#6514

Merged
mcollina merged 2 commits intomainfrom
fix/6513-route-hook-meta-return
Feb 20, 2026
Merged

fix(types): accept async route hooks in shorthand options#6514
mcollina merged 2 commits intomainfrom
fix/6513-route-hook-meta-return

Conversation

@mcollina
Copy link
Member

Problem

Route shorthand hook options were instantiated from *MetaHookHandler with the default Return type (void).

That makes eslint @typescript-eslint/no-misused-promises report async hook references like:

const preHandler = async (request: FastifyRequest) => {}
fastify.get("/", { preHandler }, async () => "ok")

Changes

  • Pass void | Promise<unknown> as the Return generic for route-level hooks in RouteShorthandOptions:
    • onRequest, preParsing, preValidation, preHandler, preSerialization, onSend, onResponse, onTimeout, onError, onRequestAbort
  • Add a type test in test/types/route.test-d.ts for async preHandler passed by reference

Notes

This is separate from #5498 / SafePromiseLike (no-floating-promises) and addresses no-misused-promises contextual typing in route hook options.

Closes #6513

@github-actions github-actions bot added the typescript TypeScript related label Feb 19, 2026
@mcollina mcollina merged commit c4a6e78 into main Feb 20, 2026
35 checks passed
@mcollina mcollina deleted the fix/6513-route-hook-meta-return branch February 20, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ESLint error/warning @typescript-eslint/no-misused-promises when passing async returning handler hook in Fastify route parameter

2 participants