Error when inferring type from db.query

When I try to use type foo = ReturnType<typeof myQuery> I get the following error Type 'MySql2PreparedQuery<PreparedQueryConfig & { execute: { [x: string]: any; matchPlayers: { [x: string]: any; playerName: { [x: string]: any; } | { [x: string]: any; }[]; } | { [x: string]: any; playerName: { ...; } | { ...; }[]; }[]; matchTeams: { ...; } | { ...; }[]; homeTeam: { ...; } | { ...; }[]; awayTeam: { ...; ...' does not satisfy the constraint '(...args: any) => any'. Type 'MySql2PreparedQuery<PreparedQueryConfig & { execute: { [x: string]: any; matchPlayers: { [x: string]: any; playerName: { [x: string]: any; } | { [x: string]: any; }[]; } | { [x: string]: any; playerName: { ...; } | { ...; }[]; }[]; matchTeams: { ...; } | { ...; }[]; homeTeam: { ...; } | { ...; }[]; awayTeam: { ...; ...' provides no match for the signature '(...args: any): any' The query is a prepared statement outside of my class, and I have the type just underneath so it can be used globally by functions inside my class.
14 Replies
rphlmr ⚡
rphlmr ⚡16mo ago
Hello, what is your drizzle orm version? Can you share your query?
mr_pablo
mr_pabloOP16mo ago
drizzle is 0.28.2 query is
const match = await db.query.matches.findFirst({
with: {
matchPlayers: {
with: {
playerName: true,
},
},
matchTeams: {
with: {
team: true,
},
},
homeTeam: true,
awayTeam: true,
},
where: eq(MultiClubMatchesSchema.matches.matchID, matchID),
});
const match = await db.query.matches.findFirst({
with: {
matchPlayers: {
with: {
playerName: true,
},
},
matchTeams: {
with: {
team: true,
},
},
homeTeam: true,
awayTeam: true,
},
where: eq(MultiClubMatchesSchema.matches.matchID, matchID),
});
well, its now prepared
rphlmr ⚡
rphlmr ⚡16mo ago
type foo = Awaited<ReturnType<typeof query['execute']>>
type foo = Awaited<ReturnType<typeof query['execute']>>
I should look at the doc if there is not already a type helper for this
mr_pablo
mr_pabloOP16mo ago
ive only seen the infermodel stuff in the docs, but thats for single tables ah, your answer works thank you! what about if i kept the query inside my class, non-prepared type match = Awaited<ReturnType<typeof matchQuery['execute']>> doesnt work, and type match = Awaited<ReturnType<typeof matchQuery>> errors ofc ideally all my statements will be prepared, but just asking out of curiosity
rphlmr ⚡
rphlmr ⚡16mo ago
Do you have an example? (quick)
mr_pablo
mr_pabloOP16mo ago
const match = await db.query.matches.findFirst({
with: {
matchPlayers: {
with: {
playerName: true,
},
},
matchTeams: {
with: {
team: true,
},
},
homeTeam: true,
awayTeam: true,
},
where: eq(MultiClubMatchesSchema.matches.matchID, matchID),
});

type match = Awaited<ReturnType<typeof matchQuery>>
const match = await db.query.matches.findFirst({
with: {
matchPlayers: {
with: {
playerName: true,
},
},
matchTeams: {
with: {
team: true,
},
},
homeTeam: true,
awayTeam: true,
},
where: eq(MultiClubMatchesSchema.matches.matchID, matchID),
});

type match = Awaited<ReturnType<typeof matchQuery>>
rphlmr ⚡
rphlmr ⚡16mo ago
To be prepared a query must be outside of a function (not 100% sure)
mr_pablo
mr_pabloOP16mo ago
yes but im asking, how can i infer the type, if the query is inside my class/function? as above type match = Awaited<ReturnType<typeof matchQuery['execute']>> works for the prepared query fine
rphlmr ⚡
rphlmr ⚡16mo ago
Inside a function or a class, I fear it is not possible if what your function returns is not the query result
mr_pablo
mr_pabloOP16mo ago
const match is the result
rphlmr ⚡
rphlmr ⚡16mo ago
ah type Match = typeof match; ? or I miss the point (sorry 😅)
mr_pablo
mr_pabloOP16mo ago
doh, yea that seems to be it
rphlmr ⚡
rphlmr ⚡16mo ago
To help the referencing, feel free to mark one of these messages as the solution: right click on a message > application > mark solution
No description
mr_pablo
mr_pabloOP16mo ago
done!
Want results from more Discord servers?
Add your server