Prisma extension typing with metrics
How can I get proper typing when using dynamic client extensions, while also using the metrics preview feature?
I'm currently on: Prisma v.4.16.2
In looking for answers I see this Github Issue that seems to indicate this is not possible, in which case I'd love some feedback about what's the best way forward: https://github.com/prisma/prisma/discussions/21123
I also see here from another metrics-related answer in Discord that the metrics feature is going away in v7 in favour of OpenTelemetry: https://github.com/prisma/prisma/discussions/27898
I will update to v7 eventually but wondering if I have a path forward with my current set up.
Reduced repro in comments
GitHub
Does $metrics typecheck when using dynamic client extensions? · pr...
Question I've enabled the metrics generator, and it works, but it doesn't typecheck, (VsCode is saying $metrics does not exist). I'm using the dynamic client extensions feature, so this...
GitHub
Deprecated metrics in Prisma 7 · prisma prisma · Discussion #27898
Question Deprecated metrics Preview feature (which will be removed in Prisma 7). What to use instead? How to reproduce (optional) No response Expected behavior (optional) No response Information ab...
3 Replies
You've gone full bot mode! Let me fetch that AI answer for you ASAP.
But anywhere where I import and use the singleton provided by
initializeDb (I store it in my Express apps global settings), my IDE says that $metrics is not available on the prisma instanceYou're correct that, as of Prisma v4.16.2, there are known issues with TypeScript typings when combining dynamic client extensions (using $extends) and the metrics preview feature. Specifically, when you extend the Prisma Client, the $metrics property is lost from the type, even though it still exists at runtime. This is confirmed in multiple GitHub issues and discussions, where users report that TypeScript throws errors like Property '$metrics' does not exist on type ... when using an extended client with metrics enabled. The only workaround is to use a type cast (as any) to access $metrics, but this sacrifices type safety and autocompletion. For example:
This is a limitation of the current Prisma type system and is not resolved in v4.16.2. There is no officially supported way to get full type safety for both dynamic client extensions and metrics in this version. The Prisma team is aware of the issue, and it is tracked in open issues, but as of now, there is no fix or recommended pattern that restores type safety in this scenario. See this issue and related comments for details.
Looking ahead, the metrics feature is being deprecated in favor of OpenTelemetry in Prisma v7, so if metrics are critical to your workflow, you should plan to migrate to the new observability stack when you upgrade. For now, if you need both features, you'll have to accept the type cast workaround or avoid using $extends on the client instance that needs metrics access.
In summary: There is no type-safe solution for this in v4.16.2; use a type cast as a workaround, and plan to migrate to OpenTelemetry in v7 for better support going forward.
✅ Great, thanks for letting us know that this solved your question!