'Prisma - use include as parameter

I have an entity with many relations and I want a function to handle the include property as parameter:

export const findById = async (id: number, include: Prisma.ClaimInclude | undefined = undefined) => {
    return dbClient.claim.findUnique({
        where: {
            id,
        },
        include,
    })
}

so I would use this function like this

const claim = await findById(claimId , { assignedTo: true, })

my problem is that now I cannot access the assignedTo property

Property 'assignedTo' does not exist on type 'Claim & {}'. Did you mean 'assignedToId'? ts(2551)

is there a way I could make this approach work, or do I need to have specific functions for each include type?

Thanks



Solution 1:[1]

This fixed my problem:

export function findById<S extends Prisma.ClaimInclude>(id: number, include: Prisma.Subset<S, Prisma.ClaimInclude>) {
  return dbClient.claim.findUnique<{ where; include: S }>({
    where: {
      id,
    },
    include,
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vallo