Skip to content
Myr

fix: correct curried produceWithPatches return type (#1249)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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 #1045

authored by Alexander Kireyev · committed by GitHub · parent 7cab3c2 · Browse files · 2 files+51 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+51 added−0 removed

  • tests:+35 added−0 removed
  • other:+16 added−0 removed

2 files+51 added−0 removed

modified:__tests__/produce.ts+35 added
753 unmodified lines
754 draft.count++755 })756 assert(f, _ as (state: ROState) => ROState)757 }758 // produceWithPatches should infer the same state type as produce (#1045),759 // just wrapped in the [state, patches, inversePatches] tuple760 {761 // curried762 const f = produceWithPatches((state: State) => {763 state.count++764 })765 assert(766 f,767 _ as (state: Immutable<State>) => readonly [State, Patch[], Patch[]]768 )769 }770 {771 // curried, with extra argument772 const f = produceWithPatches((state: State, delta: number) => {773 state.count += delta774 })775 assert(776 f,777 _ as (778 state: Immutable<State>,779 delta: number780 ) => readonly [State, Patch[], Patch[]]781 )782 }783 {784 // explicitly use generic, but curried785 const f = produceWithPatches<State, [number]>((state, delta) => {786 state.count += delta787 })788 assert(789 f,790 _ as (state: State, delta: number) => readonly [State, Patch[], Patch[]]791 )792 }793}794795it("allows for mixed property value types", () => {796 type TestReadonlyObject = {
48 unmodified lines
modified:src/types/types-external.ts+16 added
232 unmodified lines
233 */234export interface IProduceWithPatches {235 // Types copied from IProduce, wrapped with PatchesTuple236 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>237238 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */239 <State, Args extends any[]>(240 recipe: (241 state: Draft<State>,242 ...args: Args243 ) => ValidRecipeReturnType<State>,244 initialState: State245 ): (state?: State, ...args: Args) => PatchesTuple<State>246 <State>(247 recipe: (state: Draft<State>) => ValidRecipeReturnType<State>248 ): (state: State) => PatchesTuple<State>249 <State, Args extends any[]>(250 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>251 ): (state: State, ...args: Args) => PatchesTuple<State>252253 <State, Recipe extends Function>(254 recipe: Recipe,255 initialState: State256 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>
15 unmodified lines