#Type instantiation is excessively deep and possibly infinite.ts(2589)

1 messages · Page 1 of 1 (latest)

granite anvil
#

im getting Type instantiation is excessively deep and possibly infinite.ts(2589)

cyan quailBOT
#

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

  1. Circular References in Functions: When an action's return value depends on the result of ctx.runQuery or ctx.runMutation, TypeScript can't infer the return type properly, creating a circular reference.

  2. Convex Auth: The convexAuth() function can sometimes cause this error when exported directly.

  3. 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:

  1. Explicitly typing the handler function's return value:
handler: async (ctx): Promise<YourReturnType> => {
  const result = await ctx.runQuery(api.myFunctions.getSomething);
  return result;
}
  1. 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.

granite anvil
#

@cyan quail i already have quite big project, how can i know what is the root problem

cyan quailBOT
#

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

  1. 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.

  2. 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

  1. Circular imports: Look for files that import each other, directly or indirectly.

  2. API usage: Check if you're importing the full api object from _generated/api. This can sometimes cause TypeScript to inspect your entire Convex folder structure.

  3. Auth implementation: If you're using Convex Auth, try temporarily commenting out your auth.ts file to see if the error disappears, as mentioned in this Discord thread.

  4. Function return types: Look for functions that call ctx.runQuery or ctx.runMutation and return their results without explicit type annotations.

Diagnostic Techniques

  1. 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.
#
  1. Simplify imports: Try replacing Id types with GenericId from 'convex/values' instead of importing from generated files, as mentioned in this Discord thread.

  2. Check tsconfig settings: Ensure your tsconfig.json has appropriate settings, particularly for moduleResolution.

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.

Would you like me to elaborate on any of these approaches?

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: