'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?



Solution 1:[1]

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 }
}>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Austin Crim