You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Builder模式下如何正确推断Prisma Selector查询的响应类型?

问题解决:正确获取Prisma SelectorGetPayload的预期类型

问题根源

当你显式指定build方法返回类型为prisma.Prisma.SelectorFindUniqueArgs时,TypeScript会将返回值的类型拓宽为这个宽泛的接口,丢失了select字段的具体结构信息。这导致Prisma.SelectorGetPayload无法识别你仅选择了clarification字段,只能返回默认的全字段类型。

解决方案

不要直接指定宽泛的返回类型,而是用satisfies关键字确保返回值符合Prisma.SelectorFindUniqueArgs约束的同时,保留具体的字段结构类型:

class SelectorFindUniqueArgsBuilder {
  async build(selector: domain.Selector) {
    return {
      where: {
        kind: selector.kind
      },
      select: {
        clarification: true,
      }
    } satisfies prisma.Prisma.SelectorFindUniqueArgs;
  }
}

const args = await new SelectorFindUniqueArgsBuilder().build(selectors.role);

// 此时Response会正确推断为 { clarification: prisma.Clarification }
type Response = Prisma.SelectorGetPayload<typeof args>;

原理说明

satisfies关键字仅做类型合规性检查,不会拓宽返回值的类型。这样typeof args会完整保留select字段的具体结构,Prisma.SelectorGetPayload就能基于这个精准结构,正确推断出你期望的仅包含clarification的响应类型。

内容的提问来源于stack exchange,提问作者Pavel Perevezencev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 21:30:47