Skip to content
Myr

fix: new Immer().produce now has the same type as produce. Fixes #749

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

authored by Michel Weststrate · committed by Michel Weststrate · parent e3dfdcd · Browse files · 2 files+24 added−5 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+24 added−5 removed

  • tests:+18 added−1 removed
  • other:+6 added−4 removed

2 files+24 added−5 removed

modified:__tests__/produce.ts+18 added-1 removed
4 unmodified lines
5 Patch,6 nothing,7 Draft,8 Immutable,9 enableAllPlugins9 enableAllPlugins,10 Immer11} from "../src/immer"1213enableAllPlugins()14
462 unmodified lines
477 ? "[Immer] minified error nr: 6"478 : "[Immer] The first or second argument to `produce` must be a function"479 )480})481482it("#749 types Immer", () => {483 const t = {484 x: 3485 }486487 const immer = new Immer()488 const z = immer.produce(t, d => {489 d.x++490 // @ts-expect-error491 d.y = 0492 })493 expect(z.x).toBe(4)494 // @ts-expect-error495 expect(z.z).toBeUndefined()496})
modified:src/core/immerClass.ts+6 added-4 removed
40 unmodified lines
41 if (typeof config?.useProxies === "boolean")42 this.setUseProxies(config!.useProxies)43 if (typeof config?.autoFreeze === "boolean")44 this.setAutoFreeze(config!.autoFreeze)45 this.produce = this.produce.bind(this)46 this.produceWithPatches = this.produceWithPatches.bind(this)45 }4647 /**48 * The `produce` function takes a value and a "recipe function" (whose
13 unmodified lines
62 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified63 * @param {Function} patchListener - optional function that will be called with all the patches produced here64 * @returns {any} a new state, or the initial state if nothing was modified65 */68 produce(base: any, recipe?: any, patchListener?: any) {66 produce: IProduce = (base: any, recipe?: any, patchListener?: any) => {67 // curried invocation68 if (typeof base === "function" && typeof recipe !== "function") {69 const defaultBase = recipe70 recipe = base
49 unmodified lines
120 return result121 } else die(21, base)122 }123126 produceWithPatches(arg1: any, arg2?: any, arg3?: any): any {124 produceWithPatches: IProduceWithPatches = (125 arg1: any,126 arg2?: any,127 arg3?: any128 ): any => {129 if (typeof arg1 === "function") {130 return (state: any, ...args: any[]) =>131 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))132 }
97 unmodified lines