6

Anyone know what is the request and response handler type in fastify?

Now I am just using 'any', typescript eslint gave me a warning:

fastify.post('/ac', async(req: any , res: any) => {
Alvin
  • 8,219
  • 25
  • 96
  • 177

1 Answers1

15

The appropriate types you're looking for are "FastifyRequest" and "FastifyReply", respectively.

They can be imported and implemented as shown below.

import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';

fastify.post('/ac', async (req: FastifyRequest, res: FastifyReply) => {

});
Rafael
  • 499
  • 1
  • 3
  • 12
Arevi
  • 151
  • 2
  • 4
    When I try to access req.body.object, it prompted err at object - Property 'object' does not exist on type 'unknown'.ts(2339) – Alvin Sep 18 '20 at 06:29
  • 3
    https://www.fastify.io/docs/latest/TypeScript/#request is helpful (the `type CustomRequest = FastifyRequest<{ Body: .. }>` part) for getting the req body typed – kevlarr May 26 '21 at 18:57
  • 1
    Updated url for above comment: https://www.fastify.io/docs/latest/Reference/TypeScript/#request – eazy_beans Jun 18 '22 at 13:58
  • why was this so hard to find?? – Eggcellentos Dec 12 '22 at 12:03