Is there a way to save the generated response Type from a query?

I see that when you hover over the type for the response of a query, you can see what data will be returned. Is there a way to extract, or save that Type somehow, so I can use it elsewhere?
7 Replies
Jökull Sólberg
ReturnType<typeof query.run> ?
Cory
CoryOP2y ago
Yes thank you this is perfect!! Weird for some reason trying this now, it does not work? Giving a type error, something about not satisifying the contraint provides no match for the signature '(...args: any): any'
Jökull Sólberg
Can you share the code? It's probably not a function that's being passed to ReturnType
Cory
CoryOP2y ago
sure:
export const getItemOrder = db.query.item
.findFirst({
columns: {
itemOrder: true
},
where: and(eq(item.id, placeholder('id')), eq(item.orgId, placeholder('orgId')))
}).prepare();

type exampleType = ReturnType<typeof getItemOrder>;
export const getItemOrder = db.query.item
.findFirst({
columns: {
itemOrder: true
},
where: and(eq(item.id, placeholder('id')), eq(item.orgId, placeholder('orgId')))
}).prepare();

type exampleType = ReturnType<typeof getItemOrder>;
Jökull Sólberg
leave .prepare() out of the variable and call it like this getItemOrder.prepare() typeof basically "yanks" a runtime thing into the type level above it (that's how I think about it) so if you call typeof on a function you wrap it in returntype to get the return of the function --- however in your case above you're calling returntype on a result, not a function so you don't need it, just leave it out
Cory
CoryOP2y ago
hmm this sort of works but gives me the actually execution as the type, not the result really
Jökull Sólberg
Screenshot of hover?
Want results from more Discord servers?
Add your server