[typescript-fetch] Fix TS2590 in instanceOf guards for wide sanitized-name models (#23980)#23982
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
…-name models The dual name/baseName membership check added in OpenAPITools#23497 narrows the `object` parameter on every clause of the type-predicate function. For models with many required properties whose sanitized TS name differs from the JSON baseName (e.g. snake_case APIs), the accumulated candidate union grows past the compiler limit and trips TS2590 ("union type too complex"), introduced in 7.23.0. Read the membership/index-access checks through `value as Record<string, any>` so TypeScript no longer narrows the predicate on each clause, keeping the dual-key behavior from OpenAPITools#23497 while restoring compilable guards. Regenerated affected typescript-fetch samples. Fixes OpenAPITools#23980
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.
Fixes #23980
Problem
As of 7.23.0 the
typescript-fetchgenerator emitsinstanceOf<Model>type guards that fail to compile with TS2590: "Expression produces a union type that is too complex to represent" when a model has many required properties whose sanitized TS name differs from the JSONbaseName(e.g. any snake_case API).The dual
name/baseNamemembership check added in #23497 reads through thevalueparameter (typedobject) on every clause:Because the function is a type predicate (
value is Model), eachin/index-access againstvaluenarrows the parameter, and the candidate union grows on every clause. With ~15+ sanitized required fields the union crosses the compiler's internal limit and trips TS2590. Skinny models stay under the threshold, which is why it was not caught by existing samples.Fix
Read the membership and index-access checks in the
hasSanitizedNamebranch throughvalue as Record<string, any>so TypeScript no longer narrows the predicate on each clause. This keeps the dual-key (name + baseName) behavior introduced by #23497 while restoring compilable guards. The non-sanitized branch (single-key) is unchanged — it never accumulated the cross-product union and already compiles for wide models.Only
modelGeneric.mustachechanged; the regenerated samples are a mechanical consequence.Verification
masterproduces a guard that failstsc --noEmitwith TS2590.tsc --noEmit --strict, exit 0).typescript-fetchsamples (./bin/generate-samples.sh ./bin/configs/typescript-fetch-*.yaml) — 9 model files updated; the changedsnakecase-discriminatorbuild's models type-check cleanly under--strict.Verification done: 1 (no in-flight PR —
gh pr list/code-search), 2 (no claims on the issue), 3 (fix is in the.mustachetemplate + generated.ts), 4 (grepped the dual-key pattern on master, confirmed present), end-to-end repro before/after withtsc.Summary by cubic
Fixes TS2590 (“union type too complex”) in
typescript-fetchwhen generatinginstanceOf<Model>guards for models with many required fields whose TS names differ from their JSONbaseName. Guards now compile while preserving dual-key checks.valuetoRecord<string, any>for dualname/baseNamemembership and discriminator checks to stop type predicate narrowing.instanceOftype guards for discriminated unions #23497; single-key path unchanged.modelGeneric.mustache; regenerated affected samples.Written for commit 50bfcf3. Summary will update on new commits.