data types on query

Ciao, i'm using prisma 6.8.2 and @prisma/extension-accelerate 2.0.1 on prisma.io postgres db. When i write a query, i'm forced to type manually output otherwise it doesn't build. Example const res = (await dbClient.assignment.findUnique({ where: { id: idAssignment, idCompany: idCompany, }, include: { linkRegistry: { where: { status: DBStatus.ACTIVE, idCompany: idCompany, }, orderBy: [ { linkType: 'asc', }, { insTS: 'asc', }, ], include: { registry: { where: { status: DBStatus.ACTIVE, idCompany: idCompany, }, }, }, }, linkImmobile: { where: { status: DBStatus.ACTIVE, idCompany: idCompany, }, orderBy: { insTS: 'asc', }, include: { immobile: { where: { status: DBStatus.ACTIVE, idCompany: idCompany, }, }, }, }, }, })) as Assignment & { linkRegistry: (LinkRegistry & { registry?: Registry })[] linkImmobile: (LinkImmobile & { immobile?: Immobile })[] }
2 Replies
Prisma AI Help
You selected the bot's instantaneous insights. Let me compile your answer right away! You're encountering a common TypeScript type inference limitation when using Prisma with dynamic or complex include/select objects, especially when using variables or deeply nested includes. This is not specific to Prisma Accelerate, but can be more noticeable with extensions or advanced query patterns. Why does this happen? - When you inline your include/select object, Prisma Client can infer the exact return type, so you get precise types for nested relations. - When you use a variable (e.g., a shared include object), or when the structure is complex, TypeScript often widens the type, making nested relations optional or less specific. This forces you to manually cast the result, as in your example. Workarounds: 1. Use Prisma.validator for type inference
Define your include object with Prisma.validator, then use Prisma.<Model>GetPayload<typeof includeObject> to get the correct type:
const assignmentInclude = Prisma.validator<Prisma.AssignmentInclude>()({
linkRegistry: { /* ... */ },
linkImmobile: { /* ... */ },
});

type AssignmentWithRelations = Prisma.AssignmentGetPayload<{ include: typeof assignmentInclude }>;

const res = await dbClient.assignment.findUnique({
where: { id: idAssignment, idCompany: idCompany },
include: assignmentInclude,
}) as AssignmentWithRelations;

const assignmentInclude = Prisma.validator<Prisma.AssignmentInclude>()({
linkRegistry: { /* ... */ },
linkImmobile: { /* ... */ },
});

type AssignmentWithRelations = Prisma.AssignmentGetPayload<{ include: typeof assignmentInclude }>;

const res = await dbClient.assignment.findUnique({
where: { id: idAssignment, idCompany: idCompany },
include: assignmentInclude,
}) as AssignmentWithRelations;

This preserves type safety and avoids manual type assertions.
Reference: DRY include with type alters query return type? Why? Workaround? 2. Generics for dynamic includes
If you need to pass includes dynamically, you can use generics to propagate the type:
export const getAssignment = async <TInclude extends Prisma.AssignmentInclude>(
where: Prisma.AssignmentWhereUniqueInput,
include: TInclude
) => {
return dbClient.assignment.findUnique<{ include: TInclude; where: typeof where }>({
where,
include,
})
;
};

export const getAssignment = async <TInclude extends Prisma.AssignmentInclude>(
where: Prisma.AssignmentWhereUniqueInput,
include: TInclude
) => {
return dbClient.assignment.findUnique<{ include: TInclude; where: typeof where }>({
where,
include,
})
;
};

Reference: Invalid typing when dynamic include Note:
This is a known TypeScript limitation with Prisma's type inference and is being discussed in the community. Using Prisma.validator is the most robust solution for now. If you need more details or examples, let me know!
Nurul
Nurul4d ago
Hey! Did this start occurring after you upgraded to accelerate extension version 2.0.1? Do you get the same issue on accelerate extension 1.3.0?

Did you find this page helpful?