Skip to content

Commit aa24400

Browse files
authored
fix: non-enumerable (immutable) methods on Map/Set (#1069)
when an immer object includes a Map/Set, the mutating methods are replaced with ones that prevent mutation outside of the procude function.   to prevent cluttering the console when inspecting the Map/Set, these methods are set to non-enumerable. this behavior also matches that of the original methods that are replaced.
1 parent b4f71a6 commit aa24400

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/utils/common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ export function freeze<T>(obj: T, deep?: boolean): T
198198
export function freeze<T>(obj: any, deep: boolean = false): T {
199199
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj
200200
if (getArchtype(obj) > 1 /* Map or Set */) {
201-
obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any
201+
Object.defineProperties(obj, {
202+
set: {value: dontMutateFrozenCollections as any},
203+
add: {value: dontMutateFrozenCollections as any},
204+
clear: {value: dontMutateFrozenCollections as any},
205+
delete: {value: dontMutateFrozenCollections as any}
206+
})
202207
}
203208
Object.freeze(obj)
204209
if (deep)

0 commit comments

Comments
 (0)