How can I create a function that takes in a Prisma object and let the return type be based on the included relations?
My goal is that versionWithRelations
is typed as having the properties pages
, variables
, and actions
, while versionWithoutRelations
does not.
Here is my current attempt:
import { Prisma } from "@prisma/client";
const getVersion = <
Relations extends Prisma.AppVersionInclude,
App extends Prisma.AppGetPayload<{
include: { versions: { include: Relations } };
}>,
>(
app: App,
): Prisma.AppVersionGetPayload<{ include: Relations }> => {
return app.versions[0]!;
};
const withRelations = await prisma.app.findUnique({
where: { id: "foo" },
include: {
versions: {
include: {
pages: true,
variables: true,
actions: true,
},
},
},
});
const withoutRelations = await prisma.app.findUnique({
where: { id: "foo" },
include: {
versions: true,
},
});
const versionWithRelations = getVersion(withRelations!);
const versionWithoutRelations = getVersion(withoutRelations!);