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
Wrong types for FastifyRegister with custom logger #4435
Comments
Good catch! Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
Same bug for me with almost all fastify plugins (sensible, formbody, compress, secure-session). Workaround import fastifySensible from "@fastify/sensible";
import type { SensibleOptions } from "@fastify/sensible";
app.register(
fastifySensible as unknown as FastifyPluginCallback<SensibleOptions>,
{},
); |
@marcoreni Hi. The issue may be related to import { fastify } from "fastify";
import { pino } from "pino";
const server = fastify({ logger: pino() }) // <-- no configuration
const routes = async (instance: typeof server): Promise<void> => {
instance.get("/bar", {}, async (res, reply) => {
return reply.send("ok");
});
}
await server.register(routes, { // <- error on routes
prefix: "/foo",
}); However, it's possible to write your example code this way which mitigates type errors on import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
import { fastify, FastifyInstance } from "fastify";
import { pino } from "pino";
// Configure the server to use the type provider.
const server = fastify({ logger: pino() }).withTypeProvider<TypeBoxTypeProvider>()
// Ensure instance is typed as FastifyInstance
const routes = async (instance: FastifyInstance) => {
// Optional: Re-configure type provider within the plugin
const configured = instance.withTypeProvider<TypeBoxTypeProvider>()
// Define routes.
configured.get("/bar", {}, (res, reply) => reply.send("ok"));
}
// Register plugin
await server.register(routes, { prefix: "/foo" }) // ok Hope this helps! |
@jagu-sayan , I think what you're encountering is a different bug. If I'm right, it's discussed here: #4241 . @sinclairzx81 you're right, it's not related to TypeProvider per se. If I use your workaround I'll lose the TypeBox Provider type enhancements, since |
Thanks @marcoreni |
We are currently fixing nodenext issues. |
Prerequisites
Fastify version
4.10.0
Plugin version
No response
Node.js version
18.12.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.6.1
Description
We're using a custom logger (based on Pino) in our implementation. When using
withTypeProvider
, we're having some issues with Typescript and the registration of plugins / routes.If I understand correctly, FastifyRegister does not use the generic for the logger, hence we're not able to pass the custom instance inside the register function.
Steps to Reproduce
Expected Behavior
It should work :)
The text was updated successfully, but these errors were encountered: