如何在 Prisma 模型中添加包含的类型定义?

Til*_*ill 12 typescript prisma

文档中的示例如下所示:

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

但是当我想读取该属性时,getUser.posts出现以下错误:

TS2339: Property 'posts' does not exist on type 'User'.
Run Code Online (Sandbox Code Playgroud)

在哪里可以找到包含选项的正确类型定义?

Aus*_*rim 39

生成的类型不包含关系,因为默认情况下查询不返回关系。要在您的类型中包含相关模型,请使用提供的 Prisma 实用程序类型,如下所示:

import { Prisma } from '@prisma/client'

type UserWithPosts = Prisma.UserGetPayload<{
  include: { posts: true }
}>
Run Code Online (Sandbox Code Playgroud)