fix: resolve unrepresentable type references (e.g. zod z.infer) via type checker fallback#2526
Open
heunghingwan wants to merge 1 commit into
Open
fix: resolve unrepresentable type references (e.g. zod z.infer) via type checker fallback#2526heunghingwan wants to merge 1 commit into
heunghingwan wants to merge 1 commit into
Conversation
Resolving a type reference whose declaration drives the parser into third-party type-level machinery (e.g. zod's `z.infer<typeof schema>`, which is built from deep generics) crashed with "Unhandled error while creating Base Type" because the AST could not be statically re-derived. When structural re-parsing throws an unexpected error, fall back to the type checker's already-resolved type for the reference (getTypeFromTypeNode + typeToTypeNode). The fallback only accepts concrete types and never silently substitutes `any`/`unknown`, so controlled errors still surface and existing behavior is unchanged. Adds a regression test (depends on zod as a devDependency).
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.
Summary
Fixes a crash (
Unhandled error while creating Base Type) when generating aschema from types derived through third-party type-level machinery, most
notably zod's
z.infer<typeof schema>. Even the minimal case below crashed:Refs #758 (the issue was closed by #1947, but only one crash path was patched;
the generator still crashed one frame deeper — see "Root cause" below).
Problem
Any type derived via
z.infer<typeof schema>(fromz.number()up toz.discriminatedUnion(...)) crashed both the CLI and the API. This wasreproducible with the simplest possible schema and was independent of schema
complexity. Hand-written unions worked because they never reach the value/
call-expression path described below.
Root cause
To resolve
type FooType = z.infer<typeof Foo>, the parser chain traverses:TypeofNodeParserresolvestypeof Fooby re-parsing Foo's value initializer(
zod.number()) as a type.CallExpressionParserthen takes the call's returntype and round-trips it through
typeToTypeNode. For zod's deeply genericreturn types this produces a synthesized node whose
typeNamehas no resolvablesymbol, so
TypeReferenceNodeParserthrowsCannot read properties of undefined (reading 'flags')— wrapped intoUnhandledError(code 109).Crucially, the TypeScript checker already resolves the whole reference to a
concrete type (e.g.
z.infer<typeof Foo>->number). The generator simplynever asked it, preferring to re-derive the type from the AST, which is
impossible for this kind of library-level type derivation.
#1947 only narrowed the
typeArgumentsspecial-case inCallExpressionParserso the flow no longer tripped on
.types; it still fell through totypeToTypeNodeand crashed one frame deeper, so the original issue was notactually resolved.
Solution
In
TypeReferenceNodeParser, wrap structural resolution of a referenceddeclaration and, only when it crashes with an unexpected error, fall back to
the type checker's already-resolved type for the reference:
getTypeFromTypeNode(node)— let the checker fully resolve the reference(handles
z.infer<...>and similar conditional/infer chains).typeToTypeNode(...)— convert the resolved type back to an AST node andparse that instead.
Why this is safe (no behavior change for the happy/sad paths)
error instanceof UnhandledError, i.e. it onlytriggers on an actual crash (a raw exception wrapped by
ChainNodeParser). Controlled errors (UnknownNodeError,LogicError, …)propagate unchanged, so all existing error semantics are preserved.
createTypeFromCheckerrefuses to emit a schema when the checker cannot offera trustworthy concrete type: it returns
undefined(and the originalerror is rethrown) if resolution yields
any/unknownor if the convertednode fails to parse. The generator therefore never silently substitutes a
meaningless schema for a genuinely broken reference.
Result
All previously-crashing cases now produce correct schemas, e.g.:
z.number(){"type":"number"}z.object({a, b})z.object({}).strict()additionalProperties: falsez.object+z.arrayz.discriminatedUnion(...)anyOfwithconstdiscriminatorsz.enum(...)required+enumTests
test/valid-data/type-typeof-zod-infercoveringz.number,z.object(with optional + enum), andz.discriminatedUnion.tsc --noEmitandeslintclean.Notes
zod, added as adevDependency. Adependency-free test cannot reproduce this crash: a locally-declared generic
interface is structurally resolvable, so it never produces the symbol-less
synthesized node that only real library generics (like zod's) trigger. Happy
to drop the devDep / rework the test if maintainers prefer.
next(the default branch).