Which typescript type should I use for select?

Hi, this question might be confusing. Let me explain the problem, I'm building a common function for drizzle().select() somethine like
const data = await selectQuery(ctx, tableName, {id:tableName.id, data:tableName.data}); // should return {id:number,data:string}

// select
export const selectQuery = async <T extends SQLiteTable<TableConfig>>(ctx: TRPContextUser, table: T, select: SelectedFields) => {
return await drizzle(ctx.c.env.DB).select(select).from(table).all(); // returning {[x:string]:never}
};
const data = await selectQuery(ctx, tableName, {id:tableName.id, data:tableName.data}); // should return {id:number,data:string}

// select
export const selectQuery = async <T extends SQLiteTable<TableConfig>>(ctx: TRPContextUser, table: T, select: SelectedFields) => {
return await drizzle(ctx.c.env.DB).select(select).from(table).all(); // returning {[x:string]:never}
};
you can see I'm getting {[x:string]:never} when I pass {id:number;data:string} on select param.
1 Reply
Angelelz
Angelelz16mo ago
The select from statement in drizzle is a complex class that takes the table, any possible joins, the object passed to select (or the absence of it) and construct the type that will be returned based on all of that. Typescript is unable to infer the return type because the parameters you are passing to the function are not being structured in the way drizzle does it, and not being passed through to the internal drizzle type system You can just bypass all that by explicitly telling typescript what the return type is, intead of expecting it to infer it for you For example:
import { SelectResult } from "drizzle-orm/sqlite-core";

export const selectQuery = async <
T extends SQLiteTable,
Selected extends Record<string, unknown>
>(
ctx: TRPContextUser,
table: T,
select: Selected
): SelectResult<Selected, "partial", Record<T["_"]["name"], 'not-null'>> => {
return await drizzle(ctx.c.env.DB).select(select).from(table).all();
};
import { SelectResult } from "drizzle-orm/sqlite-core";

export const selectQuery = async <
T extends SQLiteTable,
Selected extends Record<string, unknown>
>(
ctx: TRPContextUser,
table: T,
select: Selected
): SelectResult<Selected, "partial", Record<T["_"]["name"], 'not-null'>> => {
return await drizzle(ctx.c.env.DB).select(select).from(table).all();
};
Want results from more Discord servers?
Add your server