fix: correct curried produceWithPatches return type#1249
Open
chatman-media wants to merge 1 commit into
Open
Conversation
The curried forms of produceWithPatches inferred the wrong type: - produceWithPatches(recipe) returned the draft type (WritableDraft<State>) in the result tuple instead of the original State - the explicit-generic form produceWithPatches<State, Args>(recipe) resolved to never and was not callable IProduceWithPatches was missing the <State, Args> curried overloads that IProduce already has. Mirror those overloads, wrapped in PatchesTuple, so the curried produceWithPatches infers the same state type as produce. Closes immerjs#1045
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
The curried forms of
produceWithPatchesinfer the wrong type, while the equivalentproduceforms work correctly (issue #1045):produceWithPatches(recipe)returns the draft type (WritableDraft<State>) in the result tuple instead of the originalState.produceWithPatches<State, Args>(recipe)resolves toneverand is not even callable.Root cause
IProduceWithPatchesis documented as "Types copied from IProduce, wrapped with PatchesTuple", but the copy is incomplete: it is missing the<State, Args>curried overloads thatIProducehas. Without them, curried calls fall through toInferCurriedFromRecipe, whose return type is the draft state (DraftState) rather than the originalState, and the explicit-generic form has no matching overload at all (hencenever).Fix
Mirror the three
<State, Args>curried overloads fromIProduceintoIProduceWithPatches, wrapping their return type inPatchesTuple. The non-curried and initial-state forms are unchanged.Tests
Added type-level assertions to
__tests__/produce.ts(alongside the existing curriedproduceassertions) covering all three curried forms. These fail onmainand pass with the fix.vitest run— all 3651 tests passvitest run --config vitest.config.build.ts(build/.d.tstests) — all passyarn buildsucceeds and the new overloads are emitted intodist/immer.d.tsCloses #1045