Zena Alhello
08/03/2022, 4:56 PMprisma[modelName].findMany(), but I get this error: This expression is not callable.
Each member of the union type has signatures, but none of those signatures are compatible with each other (edited)Lloyd Richards
08/03/2022, 5:35 PMmodelName to a union. If it's just a strong then ts might complain.
type modelName = "model1" |"model2"Zena Alhello
08/03/2022, 7:16 PM``` export async function getAllItemsOfOneClassifier({
classifier,
}: {
classifier: 'color' | 'biome';
}) {
const item = prisma[classifier];
const classifierItems = await item.findMany();
return classifierItems;
}```
Zena Alhello
08/03/2022, 7:18 PMZena Alhello
08/03/2022, 7:20 PMThis expression is not callable.
Each member of the union type '(<T extends ColorFindManyArgs>(args?: SelectSubset<T, ColorFindManyArgs> | undefined) => CheckSelect<T, PrismaPromise<Color[]>, PrismaPromise<...>>) | (<T extends BiomeFindManyArgs>(args?: SelectSubset<...> | undefined) => CheckSelect<...>)' has signatures, but none of those signatures are compatible with each other.Lloyd Richards
08/03/2022, 7:25 PMitem? You might need to use the type that Prisma provides...
classifier: prisma.color | prism.biomeLloyd Richards
08/03/2022, 7:28 PMconst items = switch(classifier){
case "color":
return prisma.color.findMany();
case "biome":
return prism.biome.findMany();
}Zena Alhello
08/03/2022, 7:34 PMfindMany()Zena Alhello
08/03/2022, 7:35 PMLloyd Richards
08/03/2022, 7:55 PMfindMany() is carefully typed to allow for the where, include etc options to work. But when its generic like this the function breaks down cause its scared you might pass an unknown variable into the props. The not so nice solution would be to just case item as any, since you know you're never going to pass any props in here. But if you did want to pass props then you could use a switch statement and use the classifer on primsa and typescript will be less angry:
export async function getAllItemsOfOneClassifier({
classifier,
}: {
classifier:
| { type: "color"; props: ColorProps }
| { type: "biome"; props: BoimeProps };
}) {
switch (classifier.type) {
case "color":
return await prisma[classifier.type].findMany(classifier.props);
case "biome":
return await prisma[classifier.type].findMany(classifier.props);
}
}
Maybe someone at Prisma can give a more generic solution using some types from prisma client, but this seems the most sensible to me 🤷♂️Zena Alhello
08/03/2022, 8:30 PM