Prisma TypeScript Interfaces Generator
prisma-generator-typescript-interfaces is a Prisma generator designed to create entirely standalone, zero-dependency TypeScript interface definitions directly from a Prisma schema. Its primary purpose is to provide type definitions for models, enums, and other schema elements without requiring the full `@prisma/client` package or the generated Prisma client itself. This makes the generated types ideal for use cases such as Data Transfer Objects (DTOs) in API layers, sharing types across different services in a monorepo, or any scenario where a lightweight, independent type declaration is preferred. The package is currently stable at version 3.1.0 and has seen consistent development, including several minor and patch releases, with a recent major version (v3.0.0) introducing notable breaking changes. A key differentiator is its extensive customizability, allowing users to configure types for various Prisma scalar types (like Date, BigInt, Decimal, Bytes) to either maintain compatibility with Prisma client's native types or to match common JSON serialization formats. It also provides granular control over relation handling and enum generation.
Common errors
-
Error: Unknown generator option: 'omitRelations'.
cause The `omitRelations` option (and `optionalRelations`) was removed in version 3.0.0.fixReplace `omitRelations` or `optionalRelations` with the new `relations` option. For example, use `relations = "none"` to omit all relations, or `relations = "optional"`. -
TS2304: Cannot find name 'BufferObject'.
cause The built-in `BufferObject` and `ArrayObject` options for `bytesType` were removed in v3.0.0.fixRemove `bytesType = "BufferObject"` (or `ArrayObject`) from your generator configuration. If you need this behavior, define a custom type using the `customTypes` option. -
TS2304: Cannot find name 'satisfies'.
cause Your project's TypeScript version is older than 4.9, and the generator's `enumType = "object"` configuration uses the `satisfies` keyword.fixUpgrade your TypeScript dependency to version 4.9 or higher, or change the `enumType` option in your generator configuration to a different value. -
Type 'Uint8Array' is not assignable to type 'Buffer'.
cause The default `bytesType` changed to `Uint8Array` in v2.0.0 for Prisma v6 compatibility, but your Prisma client (likely v5) expects `Buffer`.fixExplicitly set `bytesType = "Buffer"` in your `generator typescriptInterfaces` block in `schema.prisma` if you are using Prisma v5.
Warnings
- breaking The `omitRelations` and `optionalRelations` options were removed and replaced by a single `relations` option. Users must migrate their configurations.
- breaking The built-in `BufferObject` and `ArrayObject` types for the `bytesType` option have been removed. Attempting to use these will result in configuration errors.
- breaking When `enumType` is set to `"object"`, the generated code utilizes the TypeScript `satisfies` keyword. This introduces a breaking change for projects using TypeScript versions older than 4.9.
- breaking The default type for `Bytes` fields changed from `Buffer` to `Uint8Array` to align with Prisma v6's default behavior. This is a breaking change for projects on Prisma v5 expecting `Buffer` types.
Install
-
npm install prisma-generator-typescript-interfaces -
yarn add prisma-generator-typescript-interfaces -
pnpm add prisma-generator-typescript-interfaces
Imports
- Person
const Person = require('../src/dto/interfaces');import { Person } from '../src/dto/interfaces'; - Gender
import Gender from '../src/dto/interfaces';
import { Gender } from '../src/dto/interfaces'; - PersonJson
import { PersonJson } from '../src/dto/json-interfaces';
Quickstart
/* prisma/schema.prisma */
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
generator typescriptInterfaces {
provider = "prisma-generator-typescript-interfaces"
output = "../src/types/interfaces.ts"
prettier = true
}
enum Gender {
Male
Female
Other
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
age Int
gender Gender
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
// Then, from your terminal:
// npm install --save-dev prisma-generator-typescript-interfaces prisma typescript
// npx prisma generate
// --- Example Usage in a TypeScript file (e.g., src/app.ts) ---
import { User, Gender } from './types/interfaces'; // Path relative to the output
const newUser: User = {
id: 1,
email: 'test@example.com',
name: 'John Doe',
age: 30,
gender: Gender.Male,
createdAt: new Date(),
updatedAt: new Date()
};
function processUser(user: User) {
console.log(`Processing user: ${user.name || user.email}, Age: ${user.age}`);
if (user.gender === Gender.Female) {
console.log('User is female.');
}
}
processUser(newUser);