npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nelswebdev/prisma-generator-typescript-optionals

v1.0.4

Published

Generate zero-dependency Typescript interfaces from Prisma schema

Downloads

7

Readme

Prisma TypeScript Interfaces Generator

nelswebdev/prisma-generator-typescript-optionals is a fork of mogzol/prisma-generator-typescript-interfaces which coverts nulls to undefined and coverts null unions to optionals when optionalNullables is set to true.

This is a Prisma generator that creates zero-dependency TypeScript interfaces from Prisma schema.

Important Changes

Unit Tests I have been too lazy to fix all the different unit tests from the original repo. If you would like to and make a push request, feel to do so.
Type safety The original package keeps prisma type safety since nulls are still used. However, because this package changes that, this is no longer the case.

Usage

To use this generator, first install the package:

npm install --save-dev @nelswebdev/prisma-generator-typescript-optionals

Next add the generator to your Prisma schema:

generator typescriptInterfaces {
  provider = "prisma-generator-typescript-optionals"
}

And finally generate your Prisma schema:

npx prisma generate

By default, that will output the TypeScript interface definitions to a file called interfaces.ts in your prisma folder, but this can be changed by specifying the output option. As mentioned above, the generated types will, by default, be type-compatible with the Prisma client types. If you instead want to generate types matching the JSON.stringify-ed versions of your models, you will need to change some of the options, like so:

generator typescriptInterfaces {
  provider    = "prisma-generator-typescript-interfaces"
  dateType    = "string"
  bigIntType  = "string"
  decimalType = "string"
  bytesType   = "ArrayObject"
}

Note that bigint types don't have a default toJSON method, so the above assumes that you are converting them to strings somewhere along the line.

Example

Here is an example of a configuration that generates two separate outputs, interfaces.ts with types compatible with the Prisma client types, and a second json-interfaces.ts file with types matching the output of JSON.stringify when run on the models. Both files are output to the src/dto folder (which will be created if it doesn't exist) and are formatted using Prettier. The models in json-interfaces.ts also get a Json suffix attached to them.

Input

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

generator typescriptInterfaces {
  provider = "@nelswebdev/prisma-generator-typescript-optionals"
  output   = "../src/dto/interfaces.ts"
  prettier = true
  optionalNullables = true
}

generator typescriptInterfacesJson {
  provider    = "@nelswebdev/prisma-generator-typescript-optionals"
  output      = "../src/dto/json-interfaces.ts"
  modelSuffix = "Json"
  dateType    = "string"
  bigIntType  = "string"
  decimalType = "string"
  bytesType   = "ArrayObject"
  exportEnums = false
  prettier    = true
  optionalNullables = true
}

enum Gender {
  Male
  Female
  Other
}

model Person {
  id        Int      @id @default(autoincrement())
  name      String
  age       Int
  email     String?
  gender    Gender
  addressId Int
  address   Address  @relation(fields: [addressId], references: [id])
  friendsOf Person[] @relation("Friends")
  friends   Person[] @relation("Friends")
  data      Data?
}

model Address {
  id           Int      @id
  streetNumber Int
  streetName   String
  city         String
  isBilling    Boolean
  people       Person[]
}

model Data {
  id           String   @id @default(uuid()) @db.Uuid
  stringField  String
  booleanField Boolean
  intField     Int
  bigIntField  BigInt
  floatField   Float
  decimalField Decimal
  dateField    DateTime
  jsonField    Json
  bytesField   Bytes

  optionalStringField  String?
  optionalBooleanField Boolean?
  optionalIntField     Int?
  optionalBigIntField  BigInt?
  optionalFloatField   Float?
  optionalDecimalField Decimal?
  optionalDateField    DateTime?
  optionalJsonField    Json?
  optionalBytesField   Bytes?

  stringArrayField  String[]
  booleanArrayField Boolean[]
  intArrayField     Int[]
  bigIntArrayField  BigInt[]
  floatArrayField   Float[]
  decimalArrayField Decimal[]
  dateArrayField    DateTime[]
  jsonArrayField    Json[]
  bytesArrayField   Bytes[]

  personId Int    @unique
  person   Person @relation(fields: [personId], references: [id])
}

Output

// This file was auto-generated by prisma-generator-typescript-interfaces

export type Gender = "Male" | "Female" | "Other";

export interface Person {
  id: number;
  name: string;
  age: number;
  email?: string;
  gender: Gender;
  addressId: number;
  address?: Address;
  friendsOf?: Person[];
  friends?: Person[];
  data?: Data;
}

export interface Address {
  id: number;
  streetNumber: number;
  streetName: string;
  city: string;
  isBilling: boolean;
  people?: Person[];
}

export interface Data {
  id: string;
  stringField: string;
  booleanField: boolean;
  intField: number;
  bigIntField: bigint;
  floatField: number;
  decimalField: Decimal;
  dateField: Date;
  jsonField: JsonValue;
  bytesField: Uint8Array;
  optionalStringField?: string;
  optionalBooleanField?: boolean;
  optionalIntField?: number;
  optionalBigIntField?: bigint;
  optionalFloatField?: number;
  optionalDecimalField?: Decimal;
  optionalDateField?: Date;
  optionalJsonField?: JsonValue;
  optionalBytesField?: Uint8Array;
  stringArrayField: string[];
  booleanArrayField: boolean[];
  intArrayField: number[];
  bigIntArrayField: bigint[];
  floatArrayField: number[];
  decimalArrayField: Decimal[];
  dateArrayField: Date[];
  jsonArrayField: JsonValue[];
  bytesArrayField: Uint8Array[];
  personId: number;
  person?: Person;
}

type Decimal = { valueOf(): string };

type JsonValue =
  | string
  | number
  | boolean
  | { [key in string]?: JsonValue }
  | Array<JsonValue>
  | undefined;
// This file was auto-generated by prisma-generator-typescript-interfaces

type Gender = "Male" | "Female" | "Other";

export interface PersonJson {
  id: number;
  name: string;
  age: number;
  email?: string;
  gender: Gender;
  addressId: number;
  address?: AddressJson;
  friendsOf?: PersonJson[];
  friends?: PersonJson[];
  data?: DataJson;
}

export interface AddressJson {
  id: number;
  streetNumber: number;
  streetName: string;
  city: string;
  isBilling: boolean;
  people?: PersonJson[];
}

export interface DataJson {
  id: string;
  stringField: string;
  booleanField: boolean;
  intField: number;
  bigIntField: string;
  floatField: number;
  decimalField: string;
  dateField: string;
  jsonField: JsonValue;
  bytesField: ArrayObject;
  optionalStringField?: string;
  optionalBooleanField?: boolean;
  optionalIntField?: number;
  optionalBigIntField?: string;
  optionalFloatField?: number;
  optionalDecimalField?: string;
  optionalDateField?: string;
  optionalJsonField?: JsonValue;
  optionalBytesField?: ArrayObject;
  stringArrayField: string[];
  booleanArrayField: boolean[];
  intArrayField: number[];
  bigIntArrayField: string[];
  floatArrayField: number[];
  decimalArrayField: string[];
  dateArrayField: string[];
  jsonArrayField: JsonValue[];
  bytesArrayField: ArrayObject[];
  personId: number;
  person?: PersonJson;
}

type JsonValue =
  | string
  | number
  | boolean
  | { [key in string]?: JsonValue }
  | Array<JsonValue>
  | undefined;

type ArrayObject = { [index: number]: number } & { length?: never };

Options

| Option | Type | Default | Description | | --------------------- | :-------------------------------------------------------------------------------------: | :------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | output | string | "interfaces.ts" | The output location for the generated TypeScript interfaces. | | enumPrefix | string | "" | Prefix to add to enum types. | | enumSuffix | string | "" | Suffix to add to enum types. | | enumObjectPrefix | string | "" | Prefix to add to enum objects. Only applies when enumType is object. | | enumObjectSuffix | string | "" | Suffix to add to enum objects. Only applies when enumType is object. | | modelPrefix | string | "" | Prefix to add to model types. | | modelSuffix | string | "" | Suffix to add to model types. | | typePrefix | string | "" | Prefix to add to type types (MongoDB only). | | typeSuffix | string | "" | Suffix to add to type types (MongoDB only). | | headerComment | string | "This file was auto-generated by prisma-generator-typescript-interfaces" | Sets the header comment added to the top of the generated file. Set this to an empty string to disable the header comment. Supports multiple lines with "\n". | | modelType | "interface" \| "type" | "interface" | Controls how model definitions are generated. "interface" will create TypeScript interfaces, "type" will create TypeScript types. If using MongoDB, this also affects type definitions. | | enumType | "stringUnion" \| "enum" \| "object" | "stringUnion" | Controls how enums are generated. "object" will create an object and string union type like the Prisma client, "enum" will create TypeScript enums, "stringUnion" will just create a string union type. | | dateType | "Date" \| "string" \| "number" | "Date" | The type to use for DateTime model fields. | | bigIntType | "bigint" \| "string" \| "number" | "bigint" | The type to use for BigInt model fields. | | decimalType | "Decimal" \| "string" \| "number" | "Decimal" | The type to use for Decimal model fields. The Decimal type here is just an interface with a valueOf() function. You will need to cast to an actual Decimal type if you want to use other methods. | | bytesType | "Uint8Array" \| "Buffer" \| "ArrayObject" \| "BufferObject" \| "string" \| "number[]" | "Uint8Array" | The type to use for Bytes model fields. ArrayObject is a type which matches a JSON.stringify-ed Uint8Array. BufferObject is a type which matches a JSON.stringify-ed Buffer. | | exportEnums | boolean | true | Controls whether enum definitions are exported. If false, enums will only be defined in the module scope for use by dependent types. Respects enumType. | | optionalRelations | boolean | true | Controls whether model relation fields are optional. If true, all model relation fields will use ?: in the field definition. | | omitRelations | boolean | false | Controls whether model relation fields are omitted. If true, model definitions will not include their relations. | | optionalNullables | boolean | false | Controls whether nullable fields are optional. Nullable fields are defined with \| undefined in their type definition, but if this is true, they will use ?: and omit the undefined union. | | prettier | boolean | false | Formats the output using Prettier. Setting this to true requires that the prettier package is available. | | resolvePrettierConfig | boolean | true | Tries to find and use a Prettier config file relative to the output location. |

Developing

All the code for this generator is contained within the generator.ts file. You can build the generator by running npm install then npm run build.

Credits

This is a fork of mogzol/prisma-generator-typescript-interfaces with minimal changes. Please see the LICENSE file for more information.