fastify-type-provider-typebox icon indicating copy to clipboard operation
fastify-type-provider-typebox copied to clipboard

Add JSDoc Support

Open mikicho opened this issue 3 years ago • 5 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Unfortunately, Typescript doesn't support inline generics with JSDoc, yet. https://github.com/microsoft/TypeScript/issues/27387 But AFAIK, there should be no difference between JSDoc and Typescript on this matter. I tried to make it work without the need to override the type again and again but failed. I'm not a Typescript expert, so I would appreciate it if someone could take a look.

Motivation

Use Typebox provider in JS + JSDoc projects (typescript only as a type checker without compiling the code)

Example

No response

mikicho avatar Sep 06 '22 16:09 mikicho

It seems interesting, would you mind opening a PR with your code?

Eomm avatar Sep 11 '22 13:09 Eomm

@Eomm Thanks. Unfortunately, I couldn't make it work. I'd be glad to create an example project if needed.

mikicho avatar Sep 11 '22 15:09 mikicho

This is the way forward!!

typescript only as a type checker without compiling the code

I'd love to see fastify play nicely with TS typings in JS too...

NemoStein avatar Dec 13 '22 16:12 NemoStein

Hey guys, you can do this:

It should autocomplete the params and also can validate the types if you define a jsconfig.json with checkJs=true

/** @typedef {import('@fastify/type-provider-typebox').TypeBoxTypeProvider} TypeBoxTypeProvider */

import createFastify from 'fastify'
import { TypeBoxValidatorCompiler } from '@fastify/type-provider-typebox'
import { Type } from '@sinclair/typebox'

const fastify = createFastify()

/** @type {ReturnType<typeof fastify.withTypeProvider<TypeBoxTypeProvider>>} */
const api = fastify
  .setValidatorCompiler(TypeBoxValidatorCompiler)
  .withTypeProvider()

api.get('/', {
  schema: {
    querystring: Type.Object({
      x: Type.String(),
      y: Type.String(),
      z: Type.String()
    })
  }
}, (req) => {
  const { x } = req.query
})
image

tinchoz49 avatar Mar 10 '23 19:03 tinchoz49

Wow!

mcollina avatar Mar 12 '23 10:03 mcollina