Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add type definitions for includes in a Prisma model?

The example in the documentation looks like this:

const getUser = await prisma.user.findUnique({
  where: {
    id: 1,
  },
  include: {
    posts: {
      select: {
        title: true,
      },
    },
  },
})

But when I want to read the property getUser.posts I get the following error:

TS2339: Property 'posts' does not exist on type 'User'.

Where can I find the correct type definitions the for the includes option?

like image 469
Till Avatar asked Sep 11 '25 17:09

Till


1 Answers

The generated types do not include relations because queries don't return relations by default. To include the related models in your type, use the provided Prisma utility types like so:

import { Prisma } from '@prisma/client'

type UserWithPosts = Prisma.UserGetPayload<{
  include: { posts: true }
}>
like image 110
Austin Crim Avatar answered Sep 14 '25 09:09

Austin Crim