Correct typing when using "columns" as param

Hello awesome people at Drizzle, I'm having this problem and I don't know how to fix it. So... I have this RoleManager service that has a method called getByName that can take in columns. The problem is that I can't seem to type this query in a nice way and what I've done doesn't tickle me the right way. xD
static async getByName<TColumns extends keyof RoleModel>(
roleName: RoleModel["name"],
columns?: Partial<Record<TColumns, boolean>>,
): Promise<Pick<RoleModel, TColumns> | undefined> {
const role = await db.query.roles
.findFirst({ where: eq(roles.name, roleName), columns });

return role as unknown as Pick<RoleModel, TColumns> | undefined;
}
static async getByName<TColumns extends keyof RoleModel>(
roleName: RoleModel["name"],
columns?: Partial<Record<TColumns, boolean>>,
): Promise<Pick<RoleModel, TColumns> | undefined> {
const role = await db.query.roles
.findFirst({ where: eq(roles.name, roleName), columns });

return role as unknown as Pick<RoleModel, TColumns> | undefined;
}
2 Replies
KIPRI 🚀
KIPRI 🚀OP16mo ago
By "type this query the right way" I'm referring to the fact that if I don't use the return type that I've set up with Pick<> I don't get type narrowing through type inference based on the columns I'm providing to the method when calling it. Further investigations 🕵🏻‍♂️: findFirst() is a generic, I've tried typing "columns" there, but I'm not that smart. No dice.
Andrii Sherman
Andrii Sherman16mo ago
Sorry for late response, just got a time to answer all the questions in Discord Will bring up @bloberenober for help
Want results from more Discord servers?
Add your server