Skip to content

Commit

Permalink
fix: immerjs#785 fix type inference for produce
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Apr 19, 2021
1 parent 7a53828 commit 9c0419c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 11 additions & 0 deletions __tests__/produce.ts
Expand Up @@ -654,6 +654,9 @@ it("infers async curried", async () => {
type State = {count: number}
type ROState = Immutable<State>
const base: any = {count: 0}
const res3 = produce({a: 123}, s => {
return s as any
})
{
// basic
const res = produce(base as State, draft => {
Expand Down Expand Up @@ -734,13 +737,21 @@ it("infers async curried", async () => {
})
assert(res, _ as State | undefined)
}
{
// as any
const res = produce(base as State, draft => {
return nothing as any
})
assert(res, _ as State)
}
{
// nothing not allowed
// @ts-expect-error
produce(base as State, draft => {
return nothing
})
}

{
const f = produce((draft: State) => {})
const n = f(base as State)
Expand Down
14 changes: 7 additions & 7 deletions src/types/types-external.ts
Expand Up @@ -211,19 +211,19 @@ export interface IProduce {
initialState: State
): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>

/** Promisified dormal producer */
<Base, D = Draft<Base>>(
base: Base,
recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>,
listener?: PatchListener
): Promise<Base>

/** Normal producer */
<Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.
base: Base,
recipe: (draft: D) => ValidRecipeReturnType<D>,
listener?: PatchListener
): Base

/** Promisified dormal producer */
<Base, D = Draft<Base>>(
base: Base,
recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>,
listener?: PatchListener
): Promise<Base>
}

/**
Expand Down

0 comments on commit 9c0419c

Please sign in to comment.