PrismaP
Prisma3mo ago
3 replies
Mathias

Incorrect type inferred

Hello,

I’m running into an issue with Prisma and would appreciate some help

I’m trying to create a generic function for paginating my models with a fully generic type. Here’s the function:
protected async paginateWhere<
  S extends TSelect<ModelName> | undefined = undefined,
  I extends TInclude<ModelName> | undefined = undefined,
>(
  where: Parameters<PrismaDelegates[ModelName]['findMany']>[0]['where'],
  elementPerPage: number,
  pageNumber: number,
  choose?: { select?: S; include?: I },
  args?: TArgs<ModelName>,
): Promise<{
  items: Prisma.Result<
    typeof this.model,
    OnlyDefined<{ select: S; include: I }>,
    'findMany'
  >;
  total: number;
}> {
  const take = elementPerPage;
  const skip = (pageNumber - 1) * take;
  const [items, total] = await Promise.all([
    (this.model as any).findMany({
      where,
      skip,
      take,
      ...choose,
      ...args,
    }),
    (this.model as any).count({ where }),
  ]);

  return { items, total };
}


The problem is that the type of items ends up as any, and I can’t seem to get the correct type.

For comparison, my findManyWhere function works perfectly and returns the correct type:
protected async findManyWhere<
  S extends TSelect<ModelName> | undefined = undefined,
  I extends TInclude<ModelName> | undefined = undefined,
  O extends TOmit<ModelName> | undefined = undefined,
>(
  where: Parameters<PrismaDelegates[ModelName]['findMany']>[0]['where'],
  choose?: TChoose<ModelName, S, I, O>,
  args?: TArgs<ModelName>,
): Promise<Prisma.Result<
  typeof this.model,
  OnlyDefined<{ select: S; omit: O; include: I }>,
  'findMany'
>> {
  if (!choose) {
    return (this.model as any).findMany({ where, ...args });
  }

  return await (this.model as any).findMany({
    where,
    ...choose,
    ...args,
  })


Do you have any suggestions on how to correctly type items in this context?

Thanks!
Was this page helpful?