feat: support zod infer type and complex generic types for decorators and responses #1782
+111
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
