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

TypeScript属性不匹配报错:Promise返回值类型不符问题排查

问题

我定义了如下Report接口:

export interface Report {
      date: Date | null;
      compName: string | null;
      count: number | null;
      id: number | null;
   }

并在异步函数中指定其为Promise返回类型:

async getUserData(): Promise<Report> {
    return this.prisma.user.findMany({});
  }

但出现TypeScript错误:

Type '(GetResult<{ date: Date | null; compName: string | null; count: number | null; id: number; }, unknown> & {})[]' is missing the following properties from type 'Report': date, compName, count, id ts(2739)

请问我哪里出错了?

解决方法
  • 错误核心原因:prisma.user.findMany()返回的是数组类型(符合条件的所有用户数据集合),但你给函数指定的返回类型是单个Report对象,类型不匹配导致报错。

  • 两种修正方向:

    1. 如果需求是获取单个用户数据,将findMany()替换为findUnique()或findFirst(),同时调整返回类型兼容可能的null值:

      async getUserData(): Promise<Report | null> {
          // 示例:按id查询单个用户,需根据实际业务条件调整查询参数
          return this.prisma.user.findFirst({ 
              where: { id: 1 } 
          });
      }
      
    2. 如果需求是获取多个用户数据,修改函数的返回类型为数组形式的Promise<Report[]>,保留findMany()方法:

      async getUserData(): Promise<Report[]> {
          return this.prisma.user.findMany({});
      }
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 10:33:11