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

TypeScript only evaluating to any when using @fastify/type-provider-json-schema-to-ts #12

Closed
rmehner opened this issue Jun 20, 2022 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@rmehner
Copy link
Contributor

rmehner commented Jun 20, 2022

Hi there,

migrating to fastify v4 I was very excited for better TS support on JSON schemas. So I went ahead and tried the example provided on the docs site: https://www.fastify.io/docs/latest/Reference/Type-Providers/#json-schema-to-ts

However, if I do that, foo and bar will always be of type any. Am I holding it wrong? I was expecting the types to be number and string respectively.

import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts'

import fastify from 'fastify'

const server = fastify().withTypeProvider<JsonSchemaToTsProvider>()

server.get(
  '/route',
  {
    schema: {
      querystring: {
        type: 'object',
        properties: {
          foo: { type: 'number' },
          bar: { type: 'string' },
        },
        required: ['foo', 'bar'],
      },
    } as const
  },
  request => {
    const { bar } = request.query
    bar.push('foo') // I'd expect this to fail with `error TS2339: Property 'push' does not exist on type string`
  }
)

Thank you for your help already!

Your Environment

  • node version: 18
  • fastify version: >=4.0.0
  • typescript version: 4.7.4
@rmehner rmehner added the help wanted Extra attention is needed label Jun 20, 2022
@RafaelGSS RafaelGSS added the bug Something isn't working label Jun 20, 2022
@RafaelGSS RafaelGSS transferred this issue from fastify/help Jun 20, 2022
@sinclairzx81
Copy link

@RafaelGSS Just tested this on the TypeScript playground and the type resolution appears to be working ok (TS 4.7.4)

TypeScript Playground Link

@rmehner Just have a quick check that your tsconfig.json file s configured for strict: true.

@alphaone
Copy link

Hey, I am getting the same error as @rmehner . I can see how the linked playground example works, but when I copy the code into my project, it stops to infer the type from the schema.

Even if I use the example from the ReadMe I get type unknown :\

const plugin: FastifyPluginAsyncJsonSchemaToTs = async (fastify, _opts) => {
  fastify.get(
    '/',
    {
      schema: {
        body: {
          type: 'object',
          properties: {
            x: { type: 'string' },
            y: { type: 'number' },
            z: { type: 'boolean' },
          },
          required: ['x', 'y', 'z'],
        } as const,
      },
    },
    (req) => {
      /// Property 'x' does not exist on type 'unknown'.ts(2339)
      const { x, y, z } = req.body
    },
  )
}

I am using:

  • "node": "18.11.0"
  • "typescript": "4.8.4"
  • "@fastify/type-provider-json-schema-to-ts": "2.1.1"
  • "fastify": "4.9.2"
  • "json-schema-to-ts": "2.5.5" (as a dev dependency)

@alphaone
Copy link

Just for later reference, I found my problem. I had a wrong setting in my tsconfig.json ("noStrictGenericChecks": true,) that made the json-schema-to-ts to compute useless types.

Removing the setting lets the type inferring from json-schema work like a charm 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants