You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(type-aliases): add support for generic type aliases (#15)
The changes in this commit add support for parsing and generating code for generic type aliases in the validation schema codegen tool.
The main changes are:
1. Modify the `index.ts` file to check if any interfaces or type aliases have generic type parameters.
2. Implement the `parseGenericTypeAlias` method in the `TypeAliasParser` class to handle the parsing and code generation for generic type aliases.
3. Add a new method `createGenericTypeAliasFunction` to generate the TypeBox function definition for the generic type alias.
4. Implement the `addGenericTypeAlias` method to add the generic type alias declaration to the output file.
These changes allow the validation schema codegen tool to properly handle and generate code for generic type aliases, which is an important feature for users who need to define complex schema types.
-**Import Resolution**: Cross-file type dependencies with qualified naming and circular dependency handling
32
32
@@ -39,7 +39,7 @@ The main logic for code generation resides in the <mcfile name="index.ts" path="
39
39
The `generateCode` function in <mcfilename="index.ts"path="src/index.ts"></mcfile> orchestrates the entire code generation process:
40
40
41
41
1.**Input Processing**: Creates a `SourceFile` from input using `createSourceFileFromInput`
42
-
2.**Generic Interface Detection**: Checks for generic interfaces to determine required TypeBox imports
42
+
2.**Generic Type Detection**: Checks for generic interfaces and type aliases to determine required TypeBox imports (including `TSchema`)
43
43
3.**Output File Creation**: Creates a new output file with necessary `@sinclair/typebox` imports using `createOutputFile`
44
44
4.**Dependency Traversal**: Uses `DependencyTraversal` to analyze and sort all type dependencies
45
45
5.**Code Generation**: Processes sorted nodes using `TypeBoxPrinter` in `printSortedNodes`
@@ -113,8 +113,8 @@ The <mcfile name="base-parser.ts" path="src/parsers/base-parser.ts"></mcfile> pr
113
113
114
114
#### Specialized Parsers
115
115
116
-
1.**InterfaceParser**: <mcfilename="parse-interfaces.ts"path="src/parsers/parse-interfaces.ts"></mcfile> - Handles both regular and generic interfaces
117
-
2.**TypeAliasParser**: <mcfilename="parse-type-aliases.ts"path="src/parsers/parse-type-aliases.ts"></mcfile> - Processes type alias declarations
116
+
1.**InterfaceParser**: <mcfilename="parse-interfaces.ts"path="src/parsers/parse-interfaces.ts"></mcfile> - Handles both regular and generic interfaces using the unified `GenericTypeUtils` flow for consistency with type aliases
117
+
2.**TypeAliasParser**: <mcfilename="parse-type-aliases.ts"path="src/parsers/parse-type-aliases.ts"></mcfile> - Processes both regular and generic type alias declarations using `GenericTypeUtils.createGenericArrowFunction`
@@ -127,20 +127,42 @@ The handler system in <mcfile name="handlers/typebox" path="src/handlers/typebox
127
127
128
128
1.**Base Handlers**: Foundation classes including <mcfilename="base-type-handler.ts"path="src/handlers/typebox/base-type-handler.ts"></mcfile> and specialized base classes
-**Source Code Input**: Processes TypeScript code directly from strings with validation
182
204
-**Project Context**: Enables proper relative import resolution when working with in-memory source files
183
205
206
+
### Generic Type Support
207
+
208
+
The codebase provides comprehensive support for both generic interfaces and generic type aliases, enabling complex type transformations and reusable type definitions.
209
+
210
+
#### Generic Type Aliases
211
+
212
+
The `TypeAliasParser` handles both regular and generic type aliases through specialized processing:
213
+
214
+
1.**Type Parameter Detection**: Automatically detects type parameters using `typeAlias.getTypeParameters()`
215
+
2.**Function Generation**: Creates TypeBox functions for generic type aliases with proper parameter constraints
216
+
3.**TSchema Constraints**: Applies `TSchema` constraints to all type parameters for TypeBox compatibility
217
+
4.**Static Type Generation**: Generates corresponding TypeScript type aliases using `Static<ReturnType<typeof TypeName<T>>>`
218
+
219
+
#### Generic Interface Support
220
+
221
+
Generic interfaces are processed through the `InterfaceParser` using a consistent architectural pattern that mirrors the type alias flow:
222
+
223
+
1.**Unified Generic Processing**: The interface parser now uses the same `GenericTypeUtils.createGenericArrowFunction` flow as type aliases for consistency
224
+
2.**Raw Expression Handling**: The `InterfaceTypeHandler` returns raw TypeBox expressions for generic interfaces, allowing the parser to handle arrow function wrapping
225
+
3.**Parameter Constraint Handling**: Converts TypeScript type parameter constraints to `TSchema` constraints using shared utilities
226
+
4.**Function-Based Schema Generation**: Creates TypeBox schema functions that accept type parameters through the standardized generic arrow function pattern
227
+
5.**Type Safety Preservation**: Maintains full TypeScript type safety through proper static type aliases using `Static<ReturnType<typeof TypeName<T>>>`
228
+
6.**Architectural Consistency**: Both generic interfaces and type aliases now follow the same code generation pattern, improving maintainability and reducing duplication
229
+
230
+
#### Complex Generic Scenarios
231
+
232
+
The system supports advanced generic patterns including:
233
+
234
+
-**Multiple Type Parameters**: Functions with multiple generic parameters (e.g., `ApiResponse<T, E>`)
235
+
-**Nested Generic Types**: Generic types that reference other generic types
236
+
-**Utility Type Combinations**: Complex combinations like `Partial<Readonly<Record<K, V>>>`
237
+
-**Type Parameter Propagation**: Proper handling of type parameters across nested type references
238
+
184
239
### Interface Inheritance
185
240
186
241
The codebase provides comprehensive support for TypeScript interface inheritance through a sophisticated dependency resolution and code generation system:
-**Shared Utilities**: Provides reusable key extraction logic for Pick, Omit, and other utility type handlers to avoid code duplication
236
291
292
+
#### Generic Type Utilities
293
+
294
+
The <mcfilename="generic-type-utils.ts"path="src/utils/generic-type-utils.ts"></mcfile> module provides shared utilities for consistent generic type handling across parsers:
295
+
296
+
-**Generic Arrow Function Creation**: `createGenericArrowFunction` creates standardized arrow functions for generic types with proper type parameter constraints
297
+
-**Type Parameter Processing**: Converts TypeScript type parameters to TypeBox-compatible function parameters with `TSchema` constraints
0 commit comments