Infer TS types with `sql` operator and `db.execute`

const db = drizzle(connection);
export async function getTodos() {
const result = await db.execute<Todo>(sql`select * from ${todo}`);
return result.rows;
}
const db = drizzle(connection);
export async function getTodos() {
const result = await db.execute<Todo>(sql`select * from ${todo}`);
return result.rows;
}
result.rows is of type ExecutedQuery.rows: Row[] Is there a way to tell ts that it's of Type Todo[]?
2 Replies
Andrii Sherman
Andrii Sherman15mo ago
do you use postgres? mysql? sqlite?
matthewblewitt
matthewblewitt15mo ago
I'm using mysql currently as I'm integrating with planetscale. In the docs it mentions sql<T> not being able to perform any runtime mapping, which I understand and is fine. I just couldn't quite figure out how/if I could use a generic on execute<T>. In my example using execute<Todo> doesn't seem to affect the type of result.rows Row[] For example in kysely I can do:
const result = await sql<Todo[]>`select * from todo`.execute(db)
const result = await sql<Todo[]>`select * from todo`.execute(db)
Is it possible to do something similar in drizzle?
Want results from more Discord servers?
Add your server