fix: avoid excessive Draft recursion for readonly arrays in recursive types - #1311
Open
huytdps13400 wants to merge 1 commit into
Open
huytdps13400 wants to merge 1 commit into
huytdps13400 wants to merge 1 commit into
Conversation
… types WritableDraft only took the `Draft<T[number]>[]` shortcut for mutable arrays, so readonly arrays went through the mapped type and recursive types such as type-fest's JsonValue (which uses `readonly JsonValue[]`) still hit TS2589. Match `readonly any[]` so readonly plain arrays take the same path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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 #839
Root cause
#1197 stopped the
Draft<T>recursion for recursive types by mapping plain arrays toDraft<T[number]>[], butWritableDraftonly takes that branch forT extends any[]. Readonly arrays still go through the mapped type, so recursive types that use readonly arrays still fail withTS2589: Type instantiation is excessively deep and possibly infinite. One example is type-fest's currentJsonValue, whereJsonArray = JsonValue[] | readonly JsonValue[]:Change
WritableDraft<T>now checksT extends readonly any[], so readonly plain arrays take the sameDraft<T[number]>[]branch as mutable ones. The resulting draft type is the same as before (a mutable array of drafted elements). Readonly tuples, including variadic ones, still go through thenumber extends T["length"]/IsPlainArraychecks, so they keep their tuple shape.Verification
__tests__/type-external.tsthat uses the type-fest-shapedJsonValuewithreadonlyarrays.tsc --noEmit -p __tests__:type-external.ts(29,12): error TS2589and(30,3): error TS2589TS2802errors insrc/plugins/arrayMethods.ts, which also show up onmainwith this tsconfig.readonly T[] & {...}intersections and genericreadonly A[]states produce the same types as before. With a genericA,castDraft(a)can now be pushed into a draftedreadonly A[], which failed before.yarn test:srcandyarn test:buildpass.yarn test:flowdoesn't run on my machine (the flow binary won't spawn on arm64), and it fails the same way onmain.This fix was prepared with AI assistance (Claude Code) and verified locally with the tests above.
🤖 Generated with Claude Code