Skip to content

Commit 14d1c7b

Browse files
authored
fix(shallowReadonly): align behavior with vue-next (#741)
1 parent efb4195 commit 14d1c7b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/reactivity/readonly.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { reactive, Ref, UnwrapRef } from '.'
2-
import { isArray, isPlainObject, warn } from '../utils'
2+
import { isArray, isPlainObject, isObject, warn } from '../utils'
33
import { readonlySet } from '../utils/sets'
44
import { isReactive, observe } from './reactive'
55
import { isRef, RefImpl } from './ref'
@@ -49,6 +49,13 @@ export function readonly<T extends object>(
4949

5050
export function shallowReadonly<T extends object>(obj: T): Readonly<T>
5151
export function shallowReadonly(obj: any): any {
52+
if (!isObject(obj)) {
53+
if (__DEV__) {
54+
warn(`value cannot be made reactive: ${String(obj)}`)
55+
}
56+
return obj
57+
}
58+
5259
if (
5360
!(isPlainObject(obj) || isArray(obj)) ||
5461
(!Object.isExtensible(obj) && !isRef(obj))

test/v3/reactivity/readonly.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ describe('reactivity/readonly', () => {
382382
).not.toHaveBeenWarned()
383383
})
384384

385+
test('should not process non-object data', () => {
386+
// @ts-ignore
387+
shallowReadonly(25)
388+
expect(`value cannot be made reactive: 25`).toHaveBeenWarned()
389+
})
390+
385391
// #669
386392
test('shallowReadonly should work for refs', () => {
387393
const vm = new Vue({

0 commit comments

Comments
 (0)