Closed Willaiem closed 1 year ago
Yup, this is a known TS limitation. To get around it you need to type annotate the result value for either functions.
Thanks for reporting!
Yup, this is a known TS limitation. To get around it you need to type annotate the result value for either functions.
Can I ask you for a code example? This instruction feels ambiguous, and I've attempted type annotating the return value of a handler and I've type-annotated an entire RegisteredQuery
to no avail.
Would it hurt to add a TL;DR
to the docs considering how well-known this issue is? I'd be happy to open an example PR on the docs if I can get it fixed in my codebase.
Thanks!
Yup, this is a known TS limitation. To get around it you need to type annotate the result value for either functions.
Can I ask you for a code example? This instruction feels ambiguous, and I've attempted type annotating the return value of a handler and I've type-annotated an entire
RegisteredQuery
to no avail.Would it hurt to add a
TL;DR
to the docs considering how well-known this issue is? I'd be happy to open an example PR on the docs if I can get it fixed in my codebase.Thanks!
The workaround that I found working is by extracting the type from the desired query/mutation/action.
import { action, internalQuery } from "./_generated/server";
import { internal } from "./_generated/api";
export const getTasks = internalQuery({
args: {},
handler: async (ctx) => {
const tasks = await ctx.db.query('tasks').collect()
return tasks
}
})
type UnwrapConvex<T extends (...args: any[]) => Promise<any>> = Awaited<ReturnType<T>>
export const invokeAction = action({
args: {},
handler: async (ctx) => {
const tasks = await ctx.runQuery(internal.actions.getTasks) as UnwrapConvex<typeof getTasks>
// now the circular dependency error won't occur since we changed the source of the types to the query itself
return tasks
}
})
I wish I could just do this and call it a day:
const tasks = await ctx.runQuery(getTasks)
I wonder if this is even what @xixixao was recommending...
To get around it you need to type annotate the result value for either functions. Convex overloaded the notion of a
Function
with their specification, so it's impossible to say if what you did was what they were suggesting/not. I do like your solution though, @Willaiem. I was trying to type annotate the actual convex function types with the convex generic types. It was really painful when I got to larger documents and typescript's lsp limitations(truncation of types) kicked in. Stuff like this really needs better documentation
Thanks for the feedback and the cool helper from @Willaiem. We'll see what we can do about this issue and what to add to docs.
cc @thomasballinger
When creating the app for the Web Dev Cody's hackathon, I found that returning anything from the
ctx.runQuery
orctx.runMutation
when using the Convex Actions ends with "circularly references" error.reproduction repo: https://github.com/Willaiem/convex-ts-bug
Reproduction steps:
npm init vite@latest
and choose React + TS.npx convex dev
actions.ts
The error from the Convex CLI: