Bernat Jufré
03/07/2022, 3:16 PMtype EntityKeys = "user" | "project" | "projectType";
type PrismaDelegates = Pick<typeof db, EntityKeys>;
type Entity = {
[Key in EntityKeys]: PrismaDelegates[Key];
}[EntityKeys];
const SLUG_TO_ENTITY = {
users: "user",
projects: "project",
"project-types": "projectType"
};
// `db` is a reference to an instance of the PrismaClient
const model: Entity = db[SLUG_TO_ENTITY[entity as keyof typeof SLUG_TO_ENTITY] as EntityKeys];
Then intellisense shows correctly that model
has count
and findMany
but because their signatures are not compatible with each other I get a typing error. Any ideas?Ian Ray
03/07/2022, 6:13 PMhela
03/08/2022, 2:16 PMSLUG_TO_ENTITY
with as const
and then you probably don’t need that casting that is really not safe. It should infer that model type