Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

414
Views
Fastify Typescript request query

I'm trying to put together a simple endpoint following the Fastify with Typescript docs here:

https://www.fastify.io/docs/v3.1.x/TypeScript/

export default async function foo(fastify: any) {
       const MyInstance = new Foo(fastify.db);
       app.get<{ Querystring: IQueryString, Headers: IHeaders }>(
           "/foo",
           async (request: FastifyRequest, reply: FastifyReply) => {
              console.log(request.query); // *prints query object*
              const { queryObj } = request.query; // *Gives error: Object is of type 'unknown'*
              const result = await MyInstance.getFoo(queryObj);
              reply.status(200).send(result);
           }
       );
   }

Why do I get the error when I try to access the request.query object and how do I fix it?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

By default FastifyRequest.query's type RequestQuerystringDefault maps to unknown because one cannot guess which attributes/type you'll want to set for it.

Should you have a defined type for the query of some request, just define that request type and use it:

type MyRequest = FastifyRequest<{
  Querystring: { queryObj: MyQueryObject }
}>

then specify it as the expected request type:

 async (request: MyRequest, reply: FastifyReply) => {
   const { queryObj } = request.query // Ok
 }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!