Type of client extended with dynamic extensions
Hi! In Prisma docs they say that to get type of extended client we can use factory function and ReturnType utility as follows:
And this work fine but how I can get client type when the set of extensions is dynamic? I mean the case when extensions are provided as argument to factory function and it cannot by tell explicitly what extensions are ther?
Is this possible to handle such a case with Prisma and TypeScript?
2 Replies
Hi @Tomasz Kisiel
I'm checking in with the team to see if this is possible. I'll provide a response as soon as i have one.
Hi @Tomasz Kisiel
Types are static by definition, they can’t be determined dynamically at run time.If the list of extensions you’re applying to the Prisma Client is truly dynamic and only known at runtime, it’s not possible to determine the resulting type.
However, if the extensions are known statically (even if you’re applying them programmatically in code), you can create a utility to compute the extended client type. To do this, you would need to implement a type-level version of the reduce logic to
fold
the extensions into the base client type.Hi @RaphaelEtim, thank you for your answer. Unfortunately what I want to achieve is the first case. But if it is not possible would it be at least possible to know extended client type without creating an instance of it?
Let's say in the following case TypeScript know what is the type of
prisma
, but can I somehow get this type without creating isntance?
Okay, I think I’ve solved my problem with overriding an interface exported from a package where I want to keep the Prisma configuration and tools. Now, in the package, I have everything set up. In the application’s source code, whenever I want to use some extensions, I create a factory function and extend the interface exported from package with return type of this function. It is something like this:
Maybe it’s not the best solution, but it does its job.