-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
When using typescript with @fastify/auth, verifyBearerAuth throws a "Type 'verifyBearerAuth | undefined' is not assignable to type 'FastifyAuthFunction | FastifyAuthFunction[]'" #176
Comments
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests (we use tsd for typescript) |
To address this, you can manually check for if (!fastify.verifyBearerAuth) {
throw new Error("Fastify verifyBearerAuth decorator required");
}
fastify.route({
method: "GET",
url: "/bearer",
preHandler: fastify.auth([fastify.verifyBearerAuth]),
handler: function (_, reply) {
reply.send({ hello: "world" });
},
}); However, many plugins in the Fastify ecosystem declare these decorators as defined. I could update the module declaration to remove the possibility of declare module 'fastify' {
interface FastifyInstance {
verifyBearerAuthFactory?: fastifyBearerAuth.verifyBearerAuthFactory
verifyBearerAuth?: fastifyBearerAuth.verifyBearerAuth
}
} To this: declare module 'fastify' {
interface FastifyInstance {
verifyBearerAuthFactory: fastifyBearerAuth.verifyBearerAuthFactory
verifyBearerAuth: fastifyBearerAuth.verifyBearerAuth
}
} @mcollina what do you think? |
I would do that. |
It is marked as If it removes the |
I believe it is preferable to catch errors during the build process rather than encountering them at runtime. |
If you compare with other plugins, they exist in non-nullable methods only because they are always decorated. I don't think the current types is wrong, change it is giving a false image to the users it is always there. I don't want to be chasing it back and forth. |
It seems there might be a confusion. I agree with you. |
I think @casantosmu's solution is best for this, closing. |
Prerequisites
Fastify version
4.25.2
Plugin version
9.3.0
Node.js version
20.9.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Ubuntu 22.04.2 LTS
Description
Picking up on the sample provided on the REAME for the integration with
@fastify/auth
, if we create a route with just thefastify.verifyBearerAuth
we get a typescript errorType 'verifyBearerAuth | undefined' is not assignable to type 'FastifyAuthFunction | FastifyAuthFunction[]'. Type 'undefined' is not assignable to type 'FastifyAuthFunction | FastifyAuthFunction[]'.ts(2322)
The workaround was to add a
// @ts-ignore
Any thoughts? thanks
Steps to Reproduce
Follow the README sample and add a route with just the verifybearerauth call:
Expected Behavior
Should work without the error.
The text was updated successfully, but these errors were encountered: