Is there a way to get typed results from executing a function on Postgres?

I have a function, get_widgets that returns rows. The default signature for rows returned is Record<string, unknown>[] Is there a way to specify the row type without having to cast to unknown first?
interface Widget {
id: number
name: string
}

const widgets = (
await db.execute(
sql`select * from get_widgets()`
)
).rows // Record<string, unknown>[]
interface Widget {
id: number
name: string
}

const widgets = (
await db.execute(
sql`select * from get_widgets()`
)
).rows // Record<string, unknown>[]
Prefer not to have to do
const widgets: Widget[] = (
await db.execute(
sql`select * from get_widgets()`
)
).rows as unknown as Widget[]
const widgets: Widget[] = (
await db.execute(
sql`select * from get_widgets()`
)
).rows as unknown as Widget[]
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?