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

Extract typings generated by type providers #947

Open
its-dibo opened this issue Oct 12, 2023 · 1 comment
Open

Extract typings generated by type providers #947

its-dibo opened this issue Oct 12, 2023 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@its-dibo
Copy link

regarding to Fastify docs you can use @fastify/type-provider-json-schema-to-ts to convert the schema into a TS type, like this:

import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts'
import fastify from 'fastify'
const server = fastify().withTypeProvider<JsonSchemaToTsProvider>()

server.get('/', {schema: /* your schema*/}, (req, res)=>{
  // now req.body has the right type
})

but, if I want to separate the route logic,.
unfortunately, there is no working example

server.get('/', {schema: /* your schema*/}, route)

function route(req, res){
  // I want to use the generated type here
 let { invalidProp } = req.body
}
@its-dibo its-dibo added the help wanted Extra attention is needed label Oct 12, 2023
@Uzlopak Uzlopak changed the title apply the schema provideers to the separated routes Extract typings generated by type providers Oct 12, 2023
@Uzlopak
Copy link

Uzlopak commented Oct 12, 2023

You should actually consider the schemas to be code and not separate them from the handlers.

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

No branches or pull requests

2 participants