Type instantiation is excessively deep and possibly infinite.
Problem
When I use any of the new openai-function functionality in langchain, I get many kinds of errors on typescript and eslint also takes over 30 seconds to run. Specifically, when using the DynamicStructuredTool with this zod schema: [code block] This seems like a simple schema but somehow I get the error `Type instantiation is excessively deep and possibly infinite. Additionally, it is also giving me this error `Types have separate declarations of a private property _cached`
Error Output
error `Type instantiation is excessively deep and possibly infinite.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Refactor Zod Schema to Avoid Deep Type Instantiation
The error 'Type instantiation is excessively deep and possibly infinite' occurs due to TypeScript's type system limitations when dealing with complex recursive types. In this case, the Zod schema may have recursive definitions or overly complex structures that lead to deep type instantiation. The error regarding '_cached' suggests that there are conflicting declarations of private properties, likely due to multiple instances of the same type being defined in a way that TypeScript cannot resolve.
Awaiting Verification
Be the first to verify this fix
- 1
Simplify Zod Schema
Review and simplify the Zod schema to reduce complexity. Avoid deeply nested structures and recursive types where possible.
typescriptconst schema = z.object({ name: z.string(), age: z.number() }); - 2
Use Type Aliases
Create type aliases for complex types to help TypeScript manage type instantiation better. This can prevent excessive depth in type checks.
typescripttype User = { name: string; age: number; }; const schema = z.object<User>({ name: z.string(), age: z.number() }); - 3
Check for Recursive Types
Examine the schema for any recursive types that may lead to infinite instantiation. If found, refactor them to use a more manageable structure.
typescript// Example of avoiding recursion const schema = z.object({ parent: z.lazy(() => schema.optional()) }); - 4
Update TypeScript and ESLint Configurations
Ensure that your TypeScript and ESLint configurations are optimized for performance. Adjust the 'maxNodeModuleJsDepth' setting in tsconfig.json to limit the depth of type checks.
json{ "compilerOptions": { "maxNodeModuleJsDepth": 1 } } - 5
Test Changes
Run TypeScript and ESLint after making changes to ensure that the errors are resolved and performance has improved.
bashnpm run lint && npm run build
Validation
Confirm that the TypeScript compiler no longer throws the 'excessively deep' error and that ESLint runs within an acceptable time frame (under 5 seconds). Additionally, ensure that the application functions as expected with the modified schema.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep