Skip to content

Conversation

@lyw405
Copy link
Collaborator

@lyw405 lyw405 commented Jan 8, 2026

修复:解决 Zod schema 导致的 TypeScript 类型实例化过深错误

根本原因
类型嵌套过深: 使用 .optional().default() 链式调用创建的 Zod schema 生成了多层嵌套的类型结构 ZodDefault<ZodOptional>
递归推断触发限制: TypeScript 在推断 17 个工具的所有字段类型时,递归深度超过编译器限制(约 50 层)
累积效应: 多个工具 × 多个字段 × 多层嵌套 = 超过类型系统处理能力

解决方案
在类型推断边界使用 as any 断言,切断 TypeScript 的递归类型推断链:

安全性保证:
✅ 运行时类型安全由 Zod schema 验证保证
✅ MCP SDK 在运行时检查函数签名
✅ 仅在必要的类型边界使用 any,内部逻辑保持类型安全

fixed #38

Use type assertions (as any) to prevent TypeScript from recursively inferring deeply nested Zod types (ZodDefault<ZodOptional<...>>), which was causing TS2589 compilation errors. This approach maintains runtime type safety through Zod's schema validation while avoiding compiler recursion limits.
@hustcc hustcc merged commit ce7d952 into hustcc:main Jan 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local build FAILED

2 participants