fix(typegen): render schema edge cases#11
Draft
0xpolarzero wants to merge 5 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Overview
Fix
Typegen.fromCli()output for schema shapes that were previously rendered as invalid, unsound, or too lossy TypeScript.This is only declaration generation. It does not change runtime parsing, validation, routing, or command execution.
Issue
The previous renderer handled simple object properties and plain arrays, but missed several JSON Schema shapes that Zod emits.
Optional properties were missing
| undefined:With
exactOptionalPropertyTypes, an optional property that can be assignedundefinedneeds the explicit| undefined.Some object keys could produce invalid TypeScript:
Tuple schemas were rendered as regular arrays:
Record-like schemas also lost important information:
propertyNamesrestricts which keys are allowed. It does not make every allowed key required, so partial records needPartial<Record<K, V>>.Catchall objects need care because TypeScript
Record<string, T>applies to known keys too:The old output dropped the catchall entirely. A naive
{ required: boolean } & Record<string, string>fix would reject valid values like{ required: true, extra: "ok" }, becauseRecord<string, string>also applies to the knownrequiredkey. The generated record value is widened with the declared property value types so known properties remain assignable. This is a deliberate TypeScript approximation: it preserves a usable indexable shape, but it is not as exact as JSON Schema's "additional keys only" rule.Arrays with complex item types also needed parentheses so
[]applies to the full item type:Tests failing before fix
src/Typegen.test.tschecks optional properties:src/Typegen.test.tschecks invalid property keys:src/Typegen.test.tschecks tuples and tuple rest items:src/Typegen.test.tschecks string records and catchalls:src/Typegen.test.tschecks array item parentheses for object intersections:src/Typegen.test.tschecks partial enum records:src/Typegen.test.ts,src/Typegen.test.ts, andsrc/Typegen.test.tscheck partial literal, numeric literal, and literal-union records:src/Typegen.test.tschecks mixed finite/open key records:src/Typegen.test.tschecks required enum records:src/Typegen.test.ts,src/Typegen.test.ts,src/Typegen.test.ts, andsrc/Typegen.test.tscheck required literal, numeric literal, literal-union, and numeric-union records:src/Typegen.test.tschecks unknown property-name schemas fall back to valid record keys:src/e2e.test.tsupdates the full generated declaration snapshot so real CLI optional args render with| undefined.Regression guards
These tests cover behavior that could regress while fixing the schema renderer:
...T[]syntax;Record<string, V>;Record<unknown, V>;[].Fix
schemaToType()now delegates object rendering to a sharedobjectToType()helper instead of duplicating only the top-levelpropertiescase.The renderer now:
?: T | undefinedfor optional object properties;prefixItemsto tuple syntax;...T[];Record<string, V>;Record<K, V>;Partial<Record<K, V>>;stringfor unknown record key schemas;