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
refactor: extract string keys utility and optimize omit type handler (#10)
This commit introduces two main changes:
1. Adds a new utility function `extractStringKeys` in `key-extraction-utils.ts` to
extract string keys from a TypeScript type node. This is useful for handling
the key types in `Pick` and `Omit` operations.
2. Optimizes the `OmitTypeHandler` in `omit-type-handler.ts` by using the new
`extractStringKeys` utility and the `createTypeBoxKeys` function to generate
the TypeBox expression for the omitted keys. This simplifies the code and
makes it more readable.
The changes improve the overall code quality and maintainability of the
validation schema codegen library.
Copy file name to clipboardExpand all lines: ARCHITECTURE.md
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,8 @@ const result = await generateCode({
196
196
- <mcfilename="typebox-call.ts"path="src/utils/typebox-call.ts"></mcfile>: Contains the core logic for converting TypeScript type nodes into TypeBox `Type` expressions. `getTypeBoxType` takes a `TypeNode` as input and returns a `ts.Node` representing the equivalent TypeBox schema.
197
197
- <mcfilename="add-static-type-alias.ts"path="src/utils/add-static-type-alias.ts"></mcfile>: Generates and adds the `export type [TypeName] = Static<typeof [TypeName]>` declaration to the output source file. This declaration is essential for enabling TypeScript's static type inference from the dynamically generated TypeBox schemas, ensuring type safety at compile time.
198
198
- <mcfilename="typebox-codegen-utils.ts"path="src/utils/typebox-codegen-utils.ts"></mcfile>: Contains general utility functions that support the TypeBox code generation process, such as helper functions for string manipulation or AST node creation.
199
+
- <mcfilename="key-extraction-utils.ts"path="src/utils/key-extraction-utils.ts"></mcfile>: Provides shared utilities for extracting string literal keys from union or literal types, used by Pick and Omit type handlers to avoid code duplication.
200
+
- <mcfilename="node-type-utils.ts"path="src/utils/node-type-utils.ts"></mcfile>: Contains common Node type checking utilities for `canHandle` methods, including functions for checking SyntaxKind, TypeOperator patterns, and TypeReference patterns.
199
201
200
202
### Handlers Directory
201
203
@@ -207,6 +209,7 @@ This directory contains a collection of specialized handler modules, each respon
207
209
- <mcfilename="type-reference-base-handler.ts"path="src/handlers/typebox/reference/type-reference-base-handler.ts"></mcfile>: Specialized base class for utility type handlers that work with TypeScript type references. Provides `validateTypeReference` and `extractTypeArguments` methods for consistent handling of generic utility types like `Partial<T>`, `Pick<T, K>`, etc.
208
210
- <mcfilename="object-like-base-handler.ts"path="src/handlers/typebox/object/object-like-base-handler.ts"></mcfile>: Base class for handlers that process object-like structures (objects and interfaces). Provides `processProperties`, `extractProperties`, and `createObjectType` methods for consistent property handling and TypeBox object creation.
209
211
- <mcfilename="collection-base-handler.ts"path="src/handlers/typebox/collection/collection-base-handler.ts"></mcfile>: Base class for handlers that work with collections of types (arrays, tuples, unions, intersections). Provides `processTypeCollection`, `processSingleType`, and `validateNonEmptyCollection` methods for consistent type collection processing.
212
+
- <mcfilename="type-operator-base-handler.ts"path="src/handlers/typebox/type-operator-base-handler.ts"></mcfile>: Base class for TypeScript type operator handlers (keyof, readonly). Provides common functionality for checking operator types using `isTypeOperatorWithOperator` utility and processing inner types. Subclasses define `operatorKind`, `typeBoxMethod`, and `createTypeBoxCall` to customize behavior for specific operators.
210
213
211
214
#### Type Handler Implementations
212
215
@@ -230,15 +233,18 @@ This directory contains a collection of specialized handler modules, each respon
230
233
- <mcfilename="union-type-handler.ts"path="src/handlers/typebox/collection/union-type-handler.ts"></mcfile>: Handles TypeScript union types (e.g., `string | number`).
- <mcfilename="function-type-handler.ts"path="src/handlers/typebox/function-type-handler.ts"></mcfile>: Handles TypeScript function types and function declarations, including parameter types, optional parameters, and return types.
- <mcfilename="typeof-type-handler.ts"path="src/handlers/typebox/typeof-type-handler.ts"></mcfile>: Handles TypeScript `typeof` expressions for extracting types from values.
240
-
- <mcfilename="keyof-type-handler.ts"path="src/handlers/typebox/keyof-type-handler.ts"></mcfile>: Handles TypeScript `keyof` type operator for extracting object keys.
241
-
- <mcfilename="readonly-type-handler.ts"path="src/handlers/typebox/readonly-type-handler.ts"></mcfile>: Handles TypeScript `readonly` type modifier for creating immutable types.
242
248
- <mcfilename="type-operator-handler.ts"path="src/handlers/typebox/type-operator-handler.ts"></mcfile>: Fallback handler for other TypeScript type operators not covered by specific handlers.
243
249
- <mcfilename="type-reference-handler.ts"path="src/handlers/typebox/type-reference-handler.ts"></mcfile>: Handles references to other types (e.g., `MyType`).
0 commit comments