Open geovla93 opened 5 months ago
I have no clue why this is happening unfortunately. A PR to fix would be awesome.
Great I will fork the repo and play around with the type definitions. If I come up with a fix I will add a PR. Thanks for the feedback!
Just want to update you on this. Apparently, this issue exists with other type providers as well as you can see in this issue on the fastify-type-provider-typebox package. It seems the problem is the return type of the fastify.auth([])
which is typed with the FastifyTypeProvider
and not the specific type provider used in the project. Unfortunately, I'm not that proficient in typescript to fix this so for now I will use the suggestion in the issue to add as any
wherever I use fastify.auth([])
.
I was poking around with index-test.d.ts
and found a few things:
The passing case:
jsonSchemaToTS.route({
method: 'GET',
url: '/with-param',
schema: {body: {type: 'object', properties: {param: {type: 'string'}}, required: ['param']}},
preHandler: jsonSchemaToTS.auth([]),
handler: (req) => {
expectType<{ param: string, [x: string]: unknown }>(req.body)
}
})
The failing case (req.body
is of type unknown
):
jsonSchemaToTS.get("/test", {
preHandler: jsonSchemaToTS.auth([]),
schema: { body: { type: 'object', properties: { param: { type: 'string' } }, required: ['param'] } },
}, (req) => {
expectType<{ param: string, [x: string]: unknown }>(req.body)
})
As mentioned by @geovla93, the failing case starts to work if we do preHandler: jsonSchemaToTS.auth([]) as any
Update: just found that this also works
jsonSchemaToTS.get("/test", {
preHandler: jsonSchemaToTS.auth([]),
schema: { body: { type: 'object', properties: { param: { type: 'string' } }, required: ['param'] } },
handler: (req) => {
expectType<{ param: string, [x: string]: unknown }>(req.body)
}
})
I was experiencing a similar issue with fastify-type-provider-json-schema-to-ts
where when the auth plugin was used in the preHandler, everything was resolving to unknown. I'm not quite sure what fixed it, but ensuring I was running 4.6.1 seemed to resolve the issue for me.
Prerequisites
Fastify version
4.25.2
Plugin version
4.4.0
Node.js version
20.9.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.2.1
Description
I came across a weird bug today when I was building my API. Basically when using
@fastify/auth
plugin along withfastify-type-provider-zod
package typescript complains. I get the following error:The error is shown both in
onRequest
andpreHandler
hooks. I should mention that the error does not show up if I remove theapp.auth()
function and use the auth functions on their own or in an array.Steps to Reproduce
Create a fastify server and install the mentioned packages along with
zod
. Create two auth functions and try to use them in eitheronRequest
orpreHandler
hooks.Expected Behavior
I would like to use this package without this typescript error like this: