iamgengar.
iamgengar.
Explore posts from servers
DTDrizzle Team
Created by iamgengar. on 4/4/2025 in #help
How to make a wrapper function for a table (after version 0.39.0)?
Prior tot 0.39.0 I could easily make a wrapper function for a table, but since 0.39 the typing broke and gives multiple typing errors (even though the code does run properly). See discussion here: https://github.com/drizzle-team/drizzle-orm/issues/4069#issuecomment-2692199268 But for who doesn't like to click through: Same issue for all my table wrapper functions:
import db from '../db'
import { type Table, type SQL, } from 'drizzle-orm'

function queryTable<T extends Table>(table: T, whereClause?: SQL<unknown>) {
let query = db.select().from(table)
}
import db from '../db'
import { type Table, type SQL, } from 'drizzle-orm'

function queryTable<T extends Table>(table: T, whereClause?: SQL<unknown>) {
let query = db.select().from(table)
}
from(table) will give the error: Argument of type 'T' is not assignable to parameter of type 'TableLikeHasEmptySelection extends true ? DrizzleTypeError<"Cannot reference a data-modifying statement subquery if it doesn't contain a returning clause"> : T'. Type 'Table<TableConfig<Column<any, object, object>>>' is not assignable to type 'TableLikeHasEmptySelection extends true ? DrizzleTypeError<"Cannot reference a data-modifying statement subquery if it doesn't contain a returning clause"> : T' For now downgraded to 0.38.4, as this type bug was introduced in 0.39.0. Curious to hear how other people solved typings for their wrapper functions
2 replies
DTDrizzle Team
Created by iamgengar. on 3/1/2025 in #help
How to make function that takes in a table as parameter with correct types?
e.g. if we have the following function, which takes in an arbitrary table and executes a where clause on it:
typescript
import db from '../db'
import { type Table, type SQL, } from 'drizzle-orm'

function queryTable<T extends Table>(table: T, whereClause?: SQL<unknown>) {
let query = db.select().from(table)
if (whereClause) {
query = query.where(whereClause)
}
return query
}
typescript
import db from '../db'
import { type Table, type SQL, } from 'drizzle-orm'

function queryTable<T extends Table>(table: T, whereClause?: SQL<unknown>) {
let query = db.select().from(table)
if (whereClause) {
query = query.where(whereClause)
}
return query
}
I get the following error ("drizzle-orm": "0.39.1"): Argument of type 'T' is not assignable to parameter of type 'TableLikeHasEmptySelection<T> extends true ? DrizzleTypeError<"Cannot reference a data-modifying statement subquery if it doesn't contain a returning clause"> : T'. Type 'Table<TableConfig<Column<any, object, object>>>' is not assignable to type 'TableLikeHasEmptySelection<T> extends true ? DrizzleTypeError<"Cannot reference a data-modifying statement subquery if it doesn't contain a returning clause"> : T'
3 replies