Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not infer request property types from schema #167

Open
2 tasks done
paulius-valiunas opened this issue Aug 14, 2024 · 5 comments
Open
2 tasks done

Does not infer request property types from schema #167

paulius-valiunas opened this issue Aug 14, 2024 · 5 comments

Comments

@paulius-valiunas
Copy link

paulius-valiunas commented Aug 14, 2024

Prerequisites

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

Fastify version

4.28.1

Plugin version

3.6.0

Node.js version

20.15.0

Typescript version

5.2.2 (also tested with 5.5.4)

Operating system

Linux

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

archlinux in WSL

Description

I tried following Fastify documentation, but even when copy/pasting their example without any changes, the inferred type of request.query is unknown.

MRE:

const server = fastify().withTypeProvider<TypeBoxTypeProvider>();

server.get("/route", {
  schema: {
    querystring: Type.Object({
      foo: Type.Number(),
      bar: Type.String(),
    }),
  },
}, (request, reply) => {

  // type Query = { foo: number, bar: string }

  const { foo, bar } = request.query;
  // error TS2339: Property 'bar' does not exist on type 'unknown'.
  // error TS2339: Property 'bar' does not exist on type 'unknown'.
});

Link to code that reproduces the bug

No response

Expected Behavior

request.query should be correctly inferred to be of type { foo: string, bar: string }. Same problem with body, response, headers and params types.

@cesxhin
Copy link

cesxhin commented Sep 17, 2024

same problem

@marko1010
Copy link

marko1010 commented Oct 13, 2024

it's Fastify() not fastify()

@paulius-valiunas
Copy link
Author

paulius-valiunas commented Oct 14, 2024

it's Fastify() not fastify()

If you import it as import * as Fastify from "fastify" or import Fastify from "fastify", you're right.
If you import it as import * as fastify from "fastify" or import fastify from "fastify", it's fastify().
If you import it as import * as cthulhu from "fastify" or import cthulhu from "fastify", it's cthulhu().

@maxloo2
Copy link

maxloo2 commented Jan 10, 2025

Follow the instructions here: https://fastify.dev/docs/latest/Reference/Type-Providers/#type-definition-of-fastifyinstance--typeprovider, you need to add TypeBoxTypeProvider to FastifyInstance<T> and use that instead.

@paulius-valiunas
Copy link
Author

Follow the instructions here: fastify.dev/docs/latest/Reference/Type-Providers#type-definition-of-fastifyinstance--typeprovider, you need to add TypeBoxTypeProvider to FastifyInstance<T> and use that instead.

In my example, the type of server seems to be inferred correctly, and I'm using it in the same file where it was declared. I'm not assigning it to a different variable, passing it into a function or doing anything that could result in a type cast dropping the required generic parameters. This is the same workflow as the "Scoped type provider" example in the link you shared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants