diff --git a/packages/reactivity/__tests__/readonly.spec.ts b/packages/reactivity/__tests__/readonly.spec.ts index 1a7df080..72a873a0 100644 --- a/packages/reactivity/__tests__/readonly.spec.ts +++ b/packages/reactivity/__tests__/readonly.spec.ts @@ -474,4 +474,15 @@ describe('reactivity/readonly', () => { expect(rr.foo).toBe(r) expect(isReadonly(rr.foo)).toBe(true) }) + + test('attemptingt to write to a readonly ref nested in a reactive object should fail', () => { + const r = ref(false) + const ror = readonly(r) + const obj = reactive({ ror }) + try { + obj.ror = true + } catch (e) {} + + expect(obj.ror).toBe(false) + }) }) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 97acf063..eed9dc85 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -150,6 +150,9 @@ function createSetter(shallow = false) { receiver: object ): boolean { let oldValue = (target as any)[key] + if (isReadonly(oldValue) && isRef(oldValue)) { + return false + } if (!shallow && !isReadonly(value)) { if (!isShallow(value)) { value = toRaw(value)