I'm using Fastify with the fastify-type-provider-zod plugin:
const fastify = Fastify().withTypeProvider<ZodTypeProvider>();
And the fastify-autoload plugin:
// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: { ...opts },
});
As a result I lose the ZodTypeProvider generic in the autoloaded files:
export default async function (fastify: FastifyInstance) {
// fastify does not have the ZodTypeProvider generic here
}
I've worked around this by creating a FastifyInstanceZod type, however I'm hoping there's a better solution:
export type FastifyInstanceZod = typeof fastify;
export default async function (fastify: FastifyInstanceZod) {
// ...
}
I found the following pattern in an example route in the official Fastify demo project:
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
// route definition
}
export default plugin;
Defining the route in this way allows it to be typed as FastifyPluginAsyncZod.