fastify / fastify-type-provider-typebox

A Type Provider for Typebox
MIT License
147 stars 25 forks source link

Add JSDoc Support #38

Open mikicho opened 2 years ago

mikicho commented 2 years ago

Prerequisites

🚀 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

Eomm commented 2 years ago

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

mikicho commented 2 years ago

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

NemoStein commented 1 year ago

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

tinchoz49 commented 1 year ago

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
mcollina commented 1 year ago

Wow!