Skip to content

Commit d145128

Browse files
committed
fix(reactivity): retain readonly proxies when setting as reactive property
fix #4986
1 parent 820a143 commit d145128

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Diff for: packages/reactivity/__tests__/readonly.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,13 @@ describe('reactivity/readonly', () => {
465465
'Set operation on key "randomProperty" failed: target is readonly.'
466466
).toHaveBeenWarned()
467467
})
468+
469+
// #4986
470+
test('setting a readonly object as a property of a reactive object should retain readonly proxy', () => {
471+
const r = readonly({})
472+
const rr = reactive({}) as any
473+
rr.foo = r
474+
expect(rr.foo).toBe(r)
475+
expect(isReadonly(rr.foo)).toBe(true)
476+
})
468477
})

Diff for: packages/reactivity/src/baseHandlers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
readonlyMap,
88
reactiveMap,
99
shallowReactiveMap,
10-
shallowReadonlyMap
10+
shallowReadonlyMap,
11+
isReadonly
1112
} from './reactive'
1213
import { TrackOpTypes, TriggerOpTypes } from './operations'
1314
import {
@@ -146,7 +147,7 @@ function createSetter(shallow = false) {
146147
receiver: object
147148
): boolean {
148149
let oldValue = (target as any)[key]
149-
if (!shallow) {
150+
if (!shallow && !isReadonly(value)) {
150151
value = toRaw(value)
151152
oldValue = toRaw(oldValue)
152153
if (!isArray(target) && isRef(oldValue) && !isRef(value)) {

0 commit comments

Comments
 (0)