Prisma TypeScript Interfaces Generator

3.1.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the generator in `schema.prisma`, run the generation command, and then import and use the generated `User` interface and `Gender` enum in a TypeScript application.

/* 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);

view raw JSON →