Export extended Prisma client types

Greetings, I'm trying to export an extended Prisma client types from a library package that is meant to be use through multiple NestJS applications. This package contains: - Prisma schema - postinstall script to generate the prisma client and build the src/client.ts file with the extended client source code. Code:
// @org/database
import { PrismaClient } from '#client';
import { softDeleteExtension } from '#extensions/soft-delete.js';

type ExtendedPrismaClient = ReturnType<typeof generatePrismaClient>;
type ExtendedPrismaClientOptions = ConstructorParameters<typeof PrismaClient>[0];

function generatePrismaClient(args: SoulloopPrismaClientOptions) {
return new PrismaClient(args).$extends(softDeleteExtension);
}

export * from '#client';
export { generatePrismaClient };
export type { SoulloopPrismaClient, SoulloopPrismaClientOptions };
// @org/database
import { PrismaClient } from '#client';
import { softDeleteExtension } from '#extensions/soft-delete.js';

type ExtendedPrismaClient = ReturnType<typeof generatePrismaClient>;
type ExtendedPrismaClientOptions = ConstructorParameters<typeof PrismaClient>[0];

function generatePrismaClient(args: SoulloopPrismaClientOptions) {
return new PrismaClient(args).$extends(softDeleteExtension);
}

export * from '#client';
export { generatePrismaClient };
export type { SoulloopPrismaClient, SoulloopPrismaClientOptions };
// @org/api-1: ./src/prisma/prisma.provider.ts
import type { SoulloopPrismaClientOptions } from '@org/database'

const Options: SoulloopPrismaClientOptions // resolves to any
// @org/api-1: ./src/prisma/prisma.provider.ts
import type { SoulloopPrismaClientOptions } from '@org/database'

const Options: SoulloopPrismaClientOptions // resolves to any
3 Replies
Prisma AI Help
Prisma AI Help19h ago
You selected the bot's instantaneous insights. Let me compile your answer right away!
Prisma AI Help
Prisma AI Help19h ago
It’s expected that an extended client’s type is not PrismaClient, and you must infer/export the extended type yourself. The docs recommend wrapping client creation and exporting the return type:
// @org/database
import { PrismaClient } from '#client';
import { softDeleteExtension } from '#extensions/soft-delete.js';

export function generatePrismaClient(options?: ConstructorParameters<typeof PrismaClient>[0]) {
return new PrismaClient(options).$extends(softDeleteExtension);
}

export type ExtendedPrismaClient = ReturnType<typeof generatePrismaClient>;
export type ExtendedPrismaClientOptions = ConstructorParameters<typeof PrismaClient>[0];

export * from '#client';
// @org/database
import { PrismaClient } from '#client';
import { softDeleteExtension } from '#extensions/soft-delete.js';

export function generatePrismaClient(options?: ConstructorParameters<typeof PrismaClient>[0]) {
return new PrismaClient(options).$extends(softDeleteExtension);
}

export type ExtendedPrismaClient = ReturnType<typeof generatePrismaClient>;
export type ExtendedPrismaClientOptions = ConstructorParameters<typeof PrismaClient>[0];

export * from '#client';
(“Type of an extended client” guidance) (Type of an extended client). Then in your NestJS app:
// @org/api-1: ./src/prisma/prisma.provider.ts
import type { ExtendedPrismaClientOptions } from '@org/database';

const options: ExtendedPrismaClientOptions = { /* ... */ };
// @org/api-1: ./src/prisma/prisma.provider.ts
import type { ExtendedPrismaClientOptions } from '@org/database';

const options: ExtendedPrismaClientOptions = { /* ... */ };
If you prefer a singleton-style factory in the library, the docs also show using ReturnType with a factory function, which preserves the extended type when exported across packages (e.g., in a Turborepo, they export an extended prisma instance and types from a database package) (Type of an extended client, Export client and types). If your ExtendedPrismaClientOptions is resolving to any, ensure: - You export the type alias derived from ConstructorParameters<typeof PrismaClient>[0] from the library entrypoint. - Your consumer imports that alias (not re-deriving it). - Type generation/build order runs before publishing/consuming (as in the Turborepo guide) ([Export client and types](<https://www.prisma.io/docs/guides/turborepo#24-export-the-prism
How to use Prisma ORM with Turborepo | Prisma Documentation
Learn step-by-step how to integrate Prisma ORM with Turborepo to build modular, scalable monorepo architectures efficiently.
Prisma AI Help
Prisma AI Help19h ago
a-client-and-types>)). Note: Extensions return a new client; they don’t mutate the original, so types must flow from the factory/instance you export (Argument of type 'DynamicClientExtensionThis'…). 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.

Did you find this page helpful?