Browse Source

fix(reactivity): skip non-extensible objects when using `markRaw` (#10289)

close #10288
Artyom Tuchkov 2 năm trước cách đây
mục cha
commit
2312184bc3

+ 5 - 0
packages/reactivity/__tests__/reactive.spec.ts

@@ -277,6 +277,11 @@ describe('reactivity/reactive', () => {
     expect(isReactive(obj.bar)).toBe(false)
   })
 
+  test('markRaw should skip non-extensible objects', () => {
+    const obj = Object.seal({ foo: 1 })
+    expect(() => markRaw(obj)).not.toThrowError()
+  })
+
   test('should not observe non-extensible objects', () => {
     const obj = reactive({
       foo: Object.preventExtensions({ a: 1 }),

+ 3 - 1
packages/reactivity/src/reactive.ts

@@ -385,7 +385,9 @@ export type Raw<T> = T & { [RawSymbol]?: true }
  * @see {@link https://vuejs.org/api/reactivity-advanced.html#markraw}
  */
 export function markRaw<T extends object>(value: T): Raw<T> {
-  def(value, ReactiveFlags.SKIP, true)
+  if (Object.isExtensible(value)) {
+    def(value, ReactiveFlags.SKIP, true)
+  }
   return value
 }