fastify / fastify-type-provider-typebox

A Type Provider for Typebox
MIT License
153 stars 26 forks source link

Does not infer request property types from schema #167

Open paulius-valiunas opened 3 months ago

paulius-valiunas commented 3 months ago

Prerequisites

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 commented 2 months ago

same problem

marko1010 commented 1 month ago

it's Fastify() not fastify()

paulius-valiunas commented 1 month ago

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().