Open mikicho opened 2 years ago
It seems interesting, would you mind opening a PR with your code?
@Eomm Thanks. Unfortunately, I couldn't make it work. I'd be glad to create an example project if needed.
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...
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
})
Wow!
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