- 
                Notifications
    You must be signed in to change notification settings 
- Fork 527
feat: support zod infer type and complex generic types for decorators and responses #1782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello there mathfalcon 👋
Thank you and congrats 🎉 for opening your first PR on this project.✨
We will review the following PR soon! 👀
| Hello | 
| I'd love to see this in a test as well. So we can't accidentally break this in the future | 
| hey all, just came back from vacation, will work on tests as soon as I can | 
| @mathfalcon Were you able to get those tests working? This PR looks awesome, would love for it to go in soon. | 
| @SpinBoxx @WoH @lobabob Sorry for the delay. I had an unexpected trip, but the tests are finally done. I tried to follow the same pattern as the other test files, but let me know if my approach is incorrect. I also had to modify the files related to type generation due to other edge cases that I found while writing the tests. FYI I have one failing test when running tests locally but does not seem related to my code changes, it happens on this file  What This Commit Fixes
 Test Files Overview
 | 
| It smells a bit brittle, but seems worth a shot. | 
| @WoH can we get this into a release ASAP? This is a heavily requested feature that a lot of people would find benefit from | 
| no | 
| @WoH Thanks for getting this in. Is there anything else I can do to speed up the release process, or can you consider publishing a release candidate so that I can stop using my local link to this package? It would be really useful. | 
| We need to drop node 18, that only makes sense in a major version. For that, I want to tackle the OpenAPI 3.1 support and possibly koa 3 / express 5, although these 2 could be a minor version later, too. So review the PR I guess 😅 It won't be take a week, but not today. | 
| just pitching in to say great work @mathfalcon for bringing this in! We'll also greatly benefit from this and can remove our own workaround of type generations at build-time. We can wait a week, no problem. Just glad to hear its coming along. | 
All Submissions:
Closing issues
This closesissue #1256 which specifically mentions Zod's
z.infertype support.If this is a new feature submission:
Test plan
WIP
Problem
TSOA fails to process complex types like Zod's
z.infertypes and other generic type references, causing errors during route generation. This affects:TypeError: Cannot read properties of undefined (reading 'kind')when processing generic return types@Queries() only support 'refObject' or 'nestedObjectLiteral' typeswhen using Zod inferred typesRelated Issue: This addresses issue #1256 which specifically mentions Zod's
z.infertype support.Root Cause
The
TypeResolverclass was not properly handling cases where:Date) don't have declarations in user codez.infer<typeof Schema>) aren't resolved to their underlying object structureSolution
I implemented a two-part fix:
1. Enhanced TypeResolver (
typeResolver.ts)getModelTypeDeclarations: Added graceful handling for built-in types that don't have user code declarationscalcRefTypeName: Added proper handling for inline object types in generic type argumentstypeArgumentsToContext: Added error handling for cases where type declarations are empty2. Enhanced ParameterGenerator (
parameterGenerator.ts)getQueriesParameters: Added fallback resolution for complex types that don't initially resolve torefObjectornestedObjectLiteralTypeReferenceNodetypesTesting
z.infertypes now work with@Queries()decoratorPaginatedResponse<T>) now workDateare handled gracefullyUse Case Context
This fix was developed to support a specific but common use case:
My Setup: Kysely (auto-generates TypeScript types from DB queries) → Repository methods (strongly typed) → Services → Controllers → TSOA → OpenAPI spec → Frontend TypeScript types
The Problem: Kysely generates complex generic types that TSOA couldn't process (same issue as zod), breaking the type safety chain from database to frontend.
The Solution: This fix enables TSOA to properly resolve complex types, completing the full-stack type safety circle.
Important Notes
TypeReferenceNodetypes that weren't resolving properlyPotential Side Effects
Files Changed
packages/cli/src/metadataGeneration/typeResolver.tspackages/cli/src/metadataGeneration/parameterGenerator.tsImpact
This fix enables TSOA to work seamlessly with modern TypeScript patterns and popular libraries like Zod, making it more robust for real-world applications that rely on complex type systems.
Note: Even if this PR isn't approved, the fix works for my specific use case and maintains backward compatibility, so it can be applied locally if needed.
Screenshots
Notice how PaginationQuery is a zod infered type, and the return type of the service is a Kysely autogenerated type.


🎉 The swagger docs work properly
