Advanced usage of generated types

The generated code for Prisma Client contains several helpful types that you can use to make your application more type-safe. This page describes patterns for leveraging some of the generated types.

Note that you can find all type definitions inside the index.d.ts file inside the generated .prisma/client folder.

Operating against partial structures of your model types

When using Prisma Client, every model from your Prisma schema is translated into a dedicated TypeScript type. For example, assume you have the following User and Post models:

model User {
id Int @id
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id
author User
title String
published Boolean @default(false)
}

The Prisma Client code that's generated from this schema contains this representation of the User type:

export declare type User = {
id: string
email: string
name: string | null
}

Problem: Using variations of the generated model type

Description

In some scenarios, you may need a variation of the generated User type. For example, when you have a function that expects an instance of the User model that carries the posts relation. Or when you need a type to pass only the User model's email and name fields around in your application code.

Solution

As a solution, you can customize the generated model type using Prisma Client's helper types.

The User type only contains the model's scalar fields, but doesn't account for any relations. That's because relations are not included by default in Prisma Client queries.

However, sometimes it's useful to have a type available that includes a relation (i.e. a type that you'd get from an API call that uses include). Similarly, another useful scenario could be to have a type available that includes only a subset of the model's scalar fields (i.e. a type that you'd get from an API call that uses select).

One way of achieving this would be to define these types manually in your application code:

// Define a type that includes the relation to `Post`
type UserWithPosts = {
id: string
email: string
name: string | null
posts: Post[]
}
// Define a type that only contains a subset of the scalar fields
type UserPersonalData = {
email: string
name: string | null
}

While this is certainly feasible, this approach increases the maintenance burden upon changes to the Prisma schema as you need to manually maintain the types. A cleaner solution to this is to use the UserGetPayload type that is generated and exposed by Prisma Client:

import { UserGetPayload } from '@prisma/client'
// Define a type that includes the relation to `Post`
type UserWithPosts = UserGetPayload<{
include: { posts: true }
}>
// Define a type that only contains a subset of the scalar fields
type UserPersonalData = UserGetPayload<{
select: { email: true; name: true }
}>

The main benefits of the latter approach are:

  • Cleaner approach as it leverages Prisma Client's generated types
  • Reduced maintenance burden and improved type safety when the schema changes

Problem: Getting access to the return type of a function

Description

When doing select or include operations on your models and returning these variants from a function, it can be difficult to gain access to the return type, e.g:

// Function definition that returns a partial structure
async function getUsersWithPosts() {
const users = await prisma.user.findMany({ include: { posts: true } })
return users
}

Extracting the type that represents "users with posts" from the above code snippet requires some advanced TypeScript usage:

// Function definition that returns a partial structure
async function getUsersWithPosts() {
const users = await prisma.user.findMany({ include: { posts: true } })
return users
}
// Extract `UsersWithPosts` type with
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
type UsersWithPosts = ThenArg<ReturnType<typeof getUsersWithPosts>>
// run inside `async` function
const usersWithPosts: UsersWithPosts = await getUsersWithPosts()

Solution

With the PromiseReturnType that is exposed by Prisma Client, you can solve this more elegantly:

import { PromiseReturnType } from '@prisma/client'
type UsersWithPosts = PromiseReturnType<typeof getUsersWithPosts>
Edit this page on GitHub