-
Notifications
You must be signed in to change notification settings - Fork 528
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
base: master
Are you sure you want to change the base?
feat: support zod infer type and complex generic types for decorators and responses #1782
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
|
All Submissions:
Closing issues
This closesissue #1256 which specifically mentions Zod's
z.infer
type support.If this is a new feature submission:
Test plan
WIP
Problem
TSOA fails to process complex types like Zod's
z.infer
types 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' types
when using Zod inferred typesRelated Issue: This addresses issue #1256 which specifically mentions Zod's
z.infer
type support.Root Cause
The
TypeResolver
class 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 torefObject
ornestedObjectLiteral
TypeReferenceNode
typesTesting
z.infer
types now work with@Queries()
decoratorPaginatedResponse<T>
) now workDate
are 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
TypeReferenceNode
types that weren't resolving properlyPotential Side Effects
Files Changed
packages/cli/src/metadataGeneration/typeResolver.ts
packages/cli/src/metadataGeneration/parameterGenerator.ts
Impact
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
