alexb
alexb
DTDrizzle Team
Created by alexb on 9/18/2024 in #help
How to type a generic selection function
I've been trying to implement a generic select function but can't figure out how to type it Something like this:
async function select<T extends Something>(selectFields: T, limit: number): SomethingReturn> {
const db = await getClient()
const res = await db.select(selectFields).from(users).limit(limit)
return res
}

const users = await select({ name: users.name }, 10)
// Should be typed `{ name: string }[]`
async function select<T extends Something>(selectFields: T, limit: number): SomethingReturn> {
const db = await getClient()
const res = await db.select(selectFields).from(users).limit(limit)
return res
}

const users = await select({ name: users.name }, 10)
// Should be typed `{ name: string }[]`
What type should I use to replace Something and SomethingReturn I tried a combination of
type TSchema = ExtractTablesWithRelations<typeof schema>
type TFields = TSchema['users']['columns']
type TSchema = ExtractTablesWithRelations<typeof schema>
type TFields = TSchema['users']['columns']
and multiple combinations of PgSelect , PgSelectBase but can't get it to work properly Any help appreciated
16 replies