Skip to content

Commit a3be9df

Browse files
fix: correct curried produceWithPatches return type (#1249)
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
1 parent 7cab3c2 commit a3be9df

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

__tests__/produce.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,41 @@ it("infers curried", () => {
755755
})
756756
assert(f, _ as (state: ROState) => ROState)
757757
}
758+
// produceWithPatches should infer the same state type as produce (#1045),
759+
// just wrapped in the [state, patches, inversePatches] tuple
760+
{
761+
// curried
762+
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 argument
772+
const f = produceWithPatches((state: State, delta: number) => {
773+
state.count += delta
774+
})
775+
assert(
776+
f,
777+
_ as (
778+
state: Immutable<State>,
779+
delta: number
780+
) => readonly [State, Patch[], Patch[]]
781+
)
782+
}
783+
{
784+
// explicitly use generic, but curried
785+
const f = produceWithPatches<State, [number]>((state, delta) => {
786+
state.count += delta
787+
})
788+
assert(
789+
f,
790+
_ as (state: State, delta: number) => readonly [State, Patch[], Patch[]]
791+
)
792+
}
758793
}
759794

760795
it("allows for mixed property value types", () => {

src/types/types-external.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ export interface IProduce {
234234
export interface IProduceWithPatches {
235235
// Types copied from IProduce, wrapped with PatchesTuple
236236
<Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>
237+
238+
/** 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: Args
243+
) => ValidRecipeReturnType<State>,
244+
initialState: State
245+
): (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>
252+
237253
<State, Recipe extends Function>(
238254
recipe: Recipe,
239255
initialState: State

0 commit comments

Comments
 (0)