Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions __tests__/produce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,41 @@ it("infers curried", () => {
})
assert(f, _ as (state: ROState) => ROState)
}
// produceWithPatches should infer the same state type as produce (#1045),
// just wrapped in the [state, patches, inversePatches] tuple
{
// curried
const f = produceWithPatches((state: State) => {
state.count++
})
assert(
f,
_ as (state: Immutable<State>) => readonly [State, Patch[], Patch[]]
)
}
{
// curried, with extra argument
const f = produceWithPatches((state: State, delta: number) => {
state.count += delta
})
assert(
f,
_ as (
state: Immutable<State>,
delta: number
) => readonly [State, Patch[], Patch[]]
)
}
{
// explicitly use generic, but curried
const f = produceWithPatches<State, [number]>((state, delta) => {
state.count += delta
})
assert(
f,
_ as (state: State, delta: number) => readonly [State, Patch[], Patch[]]
)
}
}

it("allows for mixed property value types", () => {
Expand Down
16 changes: 16 additions & 0 deletions src/types/types-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ export interface IProduce {
export interface IProduceWithPatches {
// Types copied from IProduce, wrapped with PatchesTuple
<Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>

/** Curried producer that infers curried from the State generic, which is explicitly passed in. */
<State, Args extends any[]>(
recipe: (
state: Draft<State>,
...args: Args
) => ValidRecipeReturnType<State>,
initialState: State
): (state?: State, ...args: Args) => PatchesTuple<State>
<State>(
recipe: (state: Draft<State>) => ValidRecipeReturnType<State>
): (state: State) => PatchesTuple<State>
<State, Args extends any[]>(
recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>
): (state: State, ...args: Args) => PatchesTuple<State>

<State, Recipe extends Function>(
recipe: Recipe,
initialState: State
Expand Down