#Type instantiation is excessively deep and possibly infinite.ts(2589)
1 messages · Page 1 of 1 (latest)
Hi @granite anvil. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
This TypeScript error typically occurs in Convex projects due to circular type references. Based on the knowledge sources, there are a few common scenarios where this happens:
Common Causes
-
Circular References in Functions: When an action's return value depends on the result of
ctx.runQueryorctx.runMutation, TypeScript can't infer the return type properly, creating a circular reference. -
Convex Auth: The
convexAuth()function can sometimes cause this error when exported directly. -
Complex Nested Types: Deeply nested or recursive types in your schema definitions.
Solutions
For Function Return Types
If you're using ctx.runQuery or ctx.runMutation and returning their results, you can fix this by:
- Explicitly typing the handler function's return value:
handler: async (ctx): Promise<YourReturnType> => {
const result = await ctx.runQuery(api.myFunctions.getSomething);
return result;
}
- Or explicitly typing the result of the query/mutation:
handler: async (ctx) => {
const result: YourReturnType = await ctx.runQuery(api.myFunctions.getSomething);
return result;
}
Dealing with circular type inference provides more details on this approach.
For Convex Auth
If the error is related to Convex Auth, you can try this workaround:
const convexAuthFunctions: any = convexAuth({...})
export const { auth, signIn, signOut, store } = convexAuthFunctions
For Schema Definitions
If you have circular dependencies in your schema definitions, you might need to restructure your imports to avoid circular references. Make sure your dependency chain is one-directional.
If you're still having trouble, you might want to check for:
- Circular imports between files
- Complex union types in your schema
- Deeply nested object structures
Without seeing your specific code, it's hard to pinpoint the exact cause, but these are the most common scenarios in Convex projects that trigger this error.
I'm happy to help further if you can provide more details about your specific implementation!
I'm happy to help further if you can provide more details about your specific implementation!
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@granite anvil Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@cyan quail i already have quite big project, how can i know what is the root problem
Let me check the knowledge base for you again. One moment! :books:
For a large Convex project, finding the root cause of the "Type instantiation is excessively deep" error can be challenging. Here are some systematic approaches to identify the problem:
Bisection Method
-
Comment out parts of your code: Start by commenting out half of your Convex functions/files, then see if the error persists. Keep narrowing down by halving the active code until you isolate the problematic area.
-
Check recent changes: As mentioned in one of the support threads, this is often not "the type finally got big enough," but rather "a recursive type was introduced," so you may be able to bisect it by trying recent commits Discord support thread.
Common Culprits to Check
-
Circular imports: Look for files that import each other, directly or indirectly.
-
API usage: Check if you're importing the full
apiobject from_generated/api. This can sometimes cause TypeScript to inspect your entire Convex folder structure. -
Auth implementation: If you're using Convex Auth, try temporarily commenting out your
auth.tsfile to see if the error disappears, as mentioned in this Discord thread. -
Function return types: Look for functions that call
ctx.runQueryorctx.runMutationand return their results without explicit type annotations.
Diagnostic Techniques
- Copy-paste TypeScript errors: Run TypeScript in a terminal window and copy the full error messages. The error might contain more details about where the circular reference is occurring Stack article.
-
Simplify imports: Try replacing
Idtypes withGenericIdfrom 'convex/values' instead of importing from generated files, as mentioned in this Discord thread. -
Check tsconfig settings: Ensure your
tsconfig.jsonhas appropriate settings, particularly formoduleResolution.
If you're still struggling to identify the issue, you might need to create a minimal reproduction case by gradually adding back parts of your code until the error reappears, which can help pinpoint the exact cause.