How can I avoid explicitly defining type in TypeScript for a internal call query handler?
I'm loving Convex <3, and I'd like to simplify my code. I have a query handler where I define a specific response type, but I want TypeScript to automatically infer the return type, instead of explicitly specifying something like
GetMeResponse | null
.
Here’s an example of my current implementation:
Question:
How can I avoid explicitly defining GetMeResponse | null
and let TypeScript infer the types automatically for these Convex query handlers?4 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
the
getMe
query doesn't require a return type, but getMeOrThrow
does because it introduces circular inference. There's a doc about it here: https://docs.convex.dev/functions/actions#dealing-with-circular-type-inference
Hmm actually you should be able to drop the return types for bothyou're also using an unnecessary
ctx.runQuery
. we recommend helper functions https://docs.convex.dev/production/best-practices/#use-helper-functions-to-write-shared-codeBest Practices | Convex Developer Hub
Here's a collection of our recommendations on how best to use Convex to build
Thanks @erquhart and @lee