Skip to content

Commit b40b04f

Browse files
authored
fix: remove invalid curried producer type (#1216)
1 parent 4f1a96f commit b40b04f

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

__tests__/produce.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ describe("curried producer", () => {
187187
let foo = produce((s: State, a: number, b: number) => {})
188188
assert(foo, _ as Recipe)
189189
foo(_ as State, 1, 2)
190+
191+
// @ts-expect-error
192+
foo(undefined, 1, 2)
190193
}
191194

192195
// Using argument parameters:
@@ -745,6 +748,13 @@ it("infers curried", () => {
745748
const n = f(base as ROState)
746749
assert(n, _ as ROState) // yay!
747750
}
751+
{
752+
// explicitly use generic, but curried
753+
const f = produce<ROState>(draft => {
754+
draft.count++
755+
})
756+
assert(f, _ as (state: ROState) => ROState)
757+
}
748758
}
749759

750760
it("allows for mixed property value types", () => {
@@ -760,3 +770,22 @@ it("allows for mixed property value types", () => {
760770
}
761771
})
762772
})
773+
774+
it("#877 - produce with typed state generic requires initial state", () => {
775+
const reducerNoInitial = produce<{count: number}, [{type: "inc"}]>(
776+
(draft, action: {type: "inc"}) => {
777+
if (action.type === "inc") draft.count++
778+
}
779+
)
780+
const reducer = produce<{count: number}, [{type: "inc"}]>(
781+
(draft, action: {type: "inc"}) => {
782+
if (action.type === "inc") draft.count++
783+
},
784+
{count: 0}
785+
)
786+
787+
expect(reducer({count: 0}, {type: "inc"})).toEqual({count: 1})
788+
expect(reducer(undefined, {type: "inc"})).toEqual({count: 1})
789+
// @ts-expect-error runtime error without initial state
790+
expect(() => reducerNoInitial(undefined, {type: "inc"})).toThrow()
791+
})

src/types/types-external.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,6 @@ export interface IProduce {
184184
): InferCurriedFromRecipe<Recipe, false>
185185

186186
/** Curried producer that infers curried from the State generic, which is explicitly passed in. */
187-
<State>(
188-
recipe: (
189-
state: Draft<State>,
190-
initialState: State
191-
) => ValidRecipeReturnType<State>
192-
): (state?: State) => State
193187
<State, Args extends any[]>(
194188
recipe: (
195189
state: Draft<State>,

0 commit comments

Comments
 (0)