fix(reactivity): fix tracking when hasOwnProperty is called with non-string value

close #10455
close #10464
This commit is contained in:
Evan You 2024-04-14 22:46:48 +08:00
parent ca84316bfb
commit c3c5dc93fb
No known key found for this signature in database
GPG Key ID: B9D421896CA450FB
2 changed files with 19 additions and 2 deletions

View File

@ -99,6 +99,21 @@ describe('reactivity/reactive/Array', () => {
expect(fn).toHaveBeenCalledTimes(1) expect(fn).toHaveBeenCalledTimes(1)
}) })
test("should reactive when mutate array's index", () => {
const original = [1, 2, 3]
const observed = reactive(original)
let dummy
effect(() => {
dummy = observed.hasOwnProperty(0)
})
expect(dummy).toBe(true)
delete observed[0]
expect(dummy).toBe(false)
})
test('shift on Array should trigger dependency once', () => { test('shift on Array should trigger dependency once', () => {
const arr = reactive([1, 2, 3]) const arr = reactive([1, 2, 3])
const fn = vi.fn() const fn = vi.fn()

View File

@ -80,10 +80,12 @@ function createArrayInstrumentations() {
return instrumentations return instrumentations
} }
function hasOwnProperty(this: object, key: string) { function hasOwnProperty(this: object, key: unknown) {
// #10455 hasOwnProperty may be called with non-string values
key = '' + key
const obj = toRaw(this) const obj = toRaw(this)
track(obj, TrackOpTypes.HAS, key) track(obj, TrackOpTypes.HAS, key)
return obj.hasOwnProperty(key) return obj.hasOwnProperty(key as string)
} }
class BaseReactiveHandler implements ProxyHandler<Target> { class BaseReactiveHandler implements ProxyHandler<Target> {