Open sabinadams opened 2 years ago
Merged declarations must have the same type parameters, so I can't think of a way you can add a parameter to an existing type without extending the interface to create a new one. The best pattern I can come up with is do the plugin registration within a function you export, that returns a new interface which extends FastifyInstance and includes the generic parameter:
interface PrismaFastifyInstance<Client> extends FastifyInstance {
prisma: Client
}
export const register = <Client>(fastify: FastifyInstance, client: Client) : PrismaFastifyInstance<Client> => {
fastify.register(prismaPlugin, {
client,
})
return fastify
}
What are you trying to achieve, or the steps to reproduce?
I'm trying to create a plugin to
register
a new function on the Fastify instance which will hold an instance of PrismaThe goal is to be able to register the plugin and provide the PrismaClient
class
via aclient
key. Within the actual server's code, I'd expectfastify.prisma
to be of the type of the instantiated classPrismaClient
.What was the result you received?
I'm having trouble figuring out how to use generic types to ensure
fastify.prisma
is the type of the provided class.What did you expect?
After registering the plugin and providing the class via the
client
option, I'd want thedata
variable here to be of the typePrismaClient
:The result should be the same as if I had done:
Context