TypeScript complaining about using a spread operator in partial select query

TypeScript complaining about me using a spread operator in partial select query. I have the following query joining data on two tables, userData and users. The query works as intended on the client side but TypeScript is complaining about me using a spread operator ...userData, (line 3) with TS2345: Argument of type [...] is not assignable to parameter of type  SelectedFields
return await db
.select({
...userData,
username: users.username,
})
.from(userData)
.leftJoin(users, eq(userData.uid, users.uid))
.orderBy(desc(userData.currentStreak))
.limit(15)
return await db
.select({
...userData,
username: users.username,
})
.from(userData)
.leftJoin(users, eq(userData.uid, users.uid))
.orderBy(desc(userData.currentStreak))
.limit(15)
I am using "drizzle-orm": "^0.28.6"
4 Replies
Angelelz
Angelelz14mo ago
You're not supposed to spread the table directly like that. Gut you can use the getTableColumns helper: https://orm.drizzle.team/docs/goodies#get-typed-table-columns
Goodies - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
Angelelz
Angelelz14mo ago
return await db
.select({
...getTableColumns(userData),
username: users.username,
})
.from(userData)
.leftJoin(users, eq(userData.uid, users.uid))
.orderBy(desc(userData.currentStreak))
.limit(15)
return await db
.select({
...getTableColumns(userData),
username: users.username,
})
.from(userData)
.leftJoin(users, eq(userData.uid, users.uid))
.orderBy(desc(userData.currentStreak))
.limit(15)
Twiser
TwiserOP14mo ago
Oh thank you so much, didn't know about this helper! This works now.
Ahmed
Ahmed14mo ago
I've almost switched to the query API even if the query doesn't include joins! My issue has been resolved.
Want results from more Discord servers?
Add your server