Skip to content

Commit c3c5dc9

Browse files
committed
fix(reactivity): fix tracking when hasOwnProperty is called with non-string value
close #10455 close #10464
1 parent ca84316 commit c3c5dc9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ describe('reactivity/reactive/Array', () => {
9999
expect(fn).toHaveBeenCalledTimes(1)
100100
})
101101

102+
test("should reactive when mutate array's index", () => {
103+
const original = [1, 2, 3]
104+
const observed = reactive(original)
105+
106+
let dummy
107+
effect(() => {
108+
dummy = observed.hasOwnProperty(0)
109+
})
110+
111+
expect(dummy).toBe(true)
112+
113+
delete observed[0]
114+
expect(dummy).toBe(false)
115+
})
116+
102117
test('shift on Array should trigger dependency once', () => {
103118
const arr = reactive([1, 2, 3])
104119
const fn = vi.fn()

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ function createArrayInstrumentations() {
8080
return instrumentations
8181
}
8282

83-
function hasOwnProperty(this: object, key: string) {
83+
function hasOwnProperty(this: object, key: unknown) {
84+
// #10455 hasOwnProperty may be called with non-string values
85+
key = '' + key
8486
const obj = toRaw(this)
8587
track(obj, TrackOpTypes.HAS, key)
86-
return obj.hasOwnProperty(key)
88+
return obj.hasOwnProperty(key as string)
8789
}
8890

8991
class BaseReactiveHandler implements ProxyHandler<Target> {

0 commit comments

Comments
 (0)