P
Prisma2mo ago
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 };
}
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,
})
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!
3 Replies
Prisma AI Help
Prisma AI Help2mo ago
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
Mathias
MathiasOP2mo ago
No description
Nurul
Nurul4w ago
Did you find a solution to this or are you still running into this?

Did you find this page helpful?