From 42104aab12c40597f8ce89bbeed22d05fc363de6 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Fri, 16 Jul 2021 16:41:06 +0800 Subject: [PATCH] fix(readonly): align behavior with vue-next. (#765) --- src/reactivity/readonly.ts | 3 +++ test/v3/reactivity/readonly.spec.ts | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/reactivity/readonly.ts b/src/reactivity/readonly.ts index 4c1d49d5..3c7ebd95 100644 --- a/src/reactivity/readonly.ts +++ b/src/reactivity/readonly.ts @@ -44,6 +44,9 @@ type UnwrapNestedRefs = T extends Ref ? T : UnwrapRef export function readonly( target: T ): DeepReadonly> { + if (__DEV__ && !isObject(target)) { + warn(`value cannot be made reactive: ${String(target)}`) + } return target as any } diff --git a/test/v3/reactivity/readonly.spec.ts b/test/v3/reactivity/readonly.spec.ts index a645715e..ebb513a3 100644 --- a/test/v3/reactivity/readonly.spec.ts +++ b/test/v3/reactivity/readonly.spec.ts @@ -6,6 +6,7 @@ import { reactive, watch, nextTick, + readonly, } from '../../../src' const Vue = require('vue/dist/vue.common.js') @@ -386,6 +387,9 @@ describe('reactivity/readonly', () => { // @ts-ignore shallowReadonly(25) expect(`value cannot be made reactive: 25`).toHaveBeenWarned() + // @ts-ignore + readonly(25) + expect(`value cannot be made reactive: 25`).toHaveBeenWarned() }) // #669