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
Hello, what is your drizzle orm version?
Can you share your query?
drizzle is 0.28.2
query is
well, its now prepared
I should look at the doc if there is not already a type helper for this
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 curiosityDo you have an example? (quick)
To be prepared a query must be outside of a function (not 100% sure)
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
Inside a function or a class, I fear it is not possible if what your function returns is not the query result
const match
is the resultah
type Match = typeof match;
?
or I miss the point (sorry 😅)doh, yea that seems to be it
To help the referencing, feel free to mark one of these messages as the solution:
right click on a message > application > mark solution
done!