Nikola
Nikola
KKysely
Created by Nikola on 2/6/2024 in #help
Dynamic return type based on provided select array
Took 2 hours but I think I figured it out. Here's my solution for anyone else who might stumble upon this problem. Feel free to leave better/smarter/faster/shorter alternatives.
async getBillableUsers<T extends SelectExpression<DB, 'user_data'>>(select: T[]) {
return this.db
.selectFrom('user_data')
.select(select)
.where((eb) => eb.or([eb('billability_type', '=', 'Billable'), eb('billability_type', 'is', null)]))
.execute();
}
async getBillableUsers<T extends SelectExpression<DB, 'user_data'>>(select: T[]) {
return this.db
.selectFrom('user_data')
.select(select)
.where((eb) => eb.or([eb('billability_type', '=', 'Billable'), eb('billability_type', 'is', null)]))
.execute();
}
Now:
const billableUsers = await this.getBillableUsers(['id', 'work_email']);
const billableUsers = await this.getBillableUsers(['id', 'work_email']);
correctly resolves to: const billableUsers: {id: string, work_email: string}[]
3 replies