Skip to content

Commit 4da2e0d

Browse files
fix: Make applyPatches to accept readonly Patch[] (#1094)
* Demonstrate possible regression * Make `Patch[]` arrays `readonly` on the outer bounds * Limit the change to `applyPatches` --------- Co-authored-by: Minh Nguyen <2852660+NMinhNguyen@users.noreply.github.com>
1 parent 073d634 commit 4da2e0d

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

__tests__/produce.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
Immutable,
99
Immer,
1010
enableMapSet,
11-
enablePatches
11+
enablePatches,
12+
produceWithPatches
1213
} from "../src/immer"
1314

1415
enableMapSet()
@@ -162,6 +163,20 @@ it("can apply patches", () => {
162163
expect(applyPatches({}, patches)).toEqual({x: 4})
163164
})
164165

166+
it("can apply readonly patches", () => {
167+
const [, patches]: readonly [
168+
{
169+
x: number
170+
},
171+
readonly Patch[],
172+
readonly Patch[]
173+
] = produceWithPatches({x: 3}, d => {
174+
d.x++
175+
})
176+
177+
expect(applyPatches({}, patches)).toEqual({x: 4})
178+
})
179+
165180
describe("curried producer", () => {
166181
it("supports rest parameters", () => {
167182
type State = {readonly a: 1}

src/core/immerClass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class Immer implements ProducersFns {
172172
this.useStrictShallowCopy_ = value
173173
}
174174

175-
applyPatches<T extends Objectish>(base: T, patches: Patch[]): T {
175+
applyPatches<T extends Objectish>(base: T, patches: readonly Patch[]): T {
176176
// If a patch replaces the entire state, take that replacement as base
177177
// before applying patches
178178
let i: number

src/plugins/patches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export function enablePatches() {
211211
})
212212
}
213213

214-
function applyPatches_<T>(draft: T, patches: Patch[]): T {
214+
function applyPatches_<T>(draft: T, patches: readonly Patch[]): T {
215215
patches.forEach(patch => {
216216
const {path, op} = patch
217217

src/utils/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const plugins: {
2424
patches: Patch[],
2525
inversePatches: Patch[]
2626
): void
27-
applyPatches_<T>(draft: T, patches: Patch[]): T
27+
applyPatches_<T>(draft: T, patches: readonly Patch[]): T
2828
}
2929
MapSet?: {
3030
proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T

0 commit comments

Comments
 (0)