Skip to content

Commit 9817c80

Browse files
authoredSep 3, 2024··
fix(reactivity): correctly handle method calls on user-extended arrays (#11760)
close #11759
1 parent 52cdb0f commit 9817c80

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎packages/reactivity/__tests__/reactiveArray.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,27 @@ describe('reactivity/reactive/Array', () => {
724724
expect(state.things.forEach('foo', 'bar', 'baz')).toBeUndefined()
725725
expect(state.things.map('foo', 'bar', 'baz')).toEqual(['1', '2', '3'])
726726
expect(state.things.some('foo', 'bar', 'baz')).toBe(true)
727+
728+
{
729+
class Collection extends Array {
730+
find(matcher: any) {
731+
return super.find(matcher)
732+
}
733+
}
734+
735+
const state = reactive({
736+
// @ts-expect-error
737+
things: new Collection({ foo: '' }),
738+
})
739+
740+
const bar = computed(() => {
741+
return state.things.find((obj: any) => obj.foo === 'bar')
742+
})
743+
bar.value
744+
state.things[0].foo = 'bar'
745+
746+
expect(bar.value).toEqual({ foo: 'bar' })
747+
}
727748
})
728749
})
729750
})

‎packages/reactivity/src/arrayInstrumentations.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,13 @@ function apply(
242242
const needsWrap = arr !== self && !isShallow(self)
243243
// @ts-expect-error our code is limited to es2016 but user code is not
244244
const methodFn = arr[method]
245-
// @ts-expect-error
246-
if (methodFn !== arrayProto[method]) {
247-
const result = methodFn.apply(arr, args)
245+
246+
// #11759
247+
// If the method being called is from a user-extended Array, the arguments will be unknown
248+
// (unknown order and unknown parameter types). In this case, we skip the shallowReadArray
249+
// handling and directly call apply with self.
250+
if (methodFn !== arrayProto[method as any]) {
251+
const result = methodFn.apply(self, args)
248252
return needsWrap ? toReactive(result) : result
249253
}
250254

0 commit comments

Comments
 (0)
Please sign in to comment.