Having trouble inferring the correct type for my utility function:
function customUtilityFunction<
P, R, A extends unknown[]
>(
prismaArgs /* Make "where" field optional as it is already defined inside findUnique method below */,
fn: (
prismaData /* Need to dynamically determine the same type as prismaData defined inside async function below based on prismaArgs */,
...args: A
) => Promise<R>
){
return async function(...args: A){
const prismaData = await prisma.session.findUnique({
...prismaArgs,
where: {
...prismaArgs.where,
sessionId: getSessionId()
}
})
return await fn(prismaData, ...args)
}
}
const dynamicFn = customUtilityFunction(
{
include: {
user: { userId: true }
}
},
async (prismaData, arg1, arg2) => {
// ^? (parameter) prismaData: { ...sessionField, user: { userId: string } }
}
)
dynamicFn(arg1, arg2)
Successfully achieved type inference on prismaData, but used methods like casting and any that are not best practices. Seeking a more proper solution.