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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSDoc Support #38

Open
2 tasks done
mikicho opened this issue Sep 6, 2022 · 5 comments
Open
2 tasks done

Add JSDoc Support #38

mikicho opened this issue Sep 6, 2022 · 5 comments

Comments

@mikicho
Copy link

mikicho commented Sep 6, 2022

Prerequisites

  • I have written a descriptive issue title
  • 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. microsoft/TypeScript#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
Copy link
Member

Eomm commented Sep 11, 2022

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

@mikicho
Copy link
Author

mikicho commented Sep 11, 2022

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

@NemoStein
Copy link

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
Copy link

tinchoz49 commented Mar 10, 2023

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
Copy link
Member

Wow!

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

5 participants