#6974·fastify

Route hooks binding types are broken since v5.8.0 (TypeScript only)

Author: greguzCreated Aug 24, 2026Updated Aug 24, 2026

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.8.0 (TypeScript 5.9.3)

Plugin version

No response

Node.js version

26.3.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

26.6.2

Description

Since PR #6514 binding types (the this part) are broken and aren't resolved correctly. Actual code runs correctly, this is just a typing problem.

The problem boils down to the addition of this type utility:

typescript
type RouteShorthandHook<T extends (...args: any) => any> = (
  ...args: Parameters<T>
) => void | Promise<unknown>

AFAIK you can't use this spread syntax while keeping the this type preserved/inherited.

I can confirm that explicitly inferring the context and then passing it to the function type will fix the issue:

typescript
type RouteShorthandHook<T extends (this: infer C, ...args: any) => any> = (
  this: C,
  ...args: Parameters<T>
) => void | Promise<unknown>

Is there any particular reason this was overlooked?

Example code:

typescript
import Fastify from 'fastify'

const fastify = Fastify()

fastify.route({
  method: 'GET',
  url: '/',
  async onRequest() {
    this.log.error('here this type error') // <-- typescript will cry here since Fasticy v5.8.0
  },
  async handler(request, reply) {
    return { hello: 'world' }
  },
})

Link to code that reproduces the bug

Do I need to create a repo for a TypeScript related problem?

Expected Behavior

Route hooks resolves their context correctly.