diff --git a/src/__tests__/reactive.spec.ts.ts b/src/__tests__/reactive.spec.ts similarity index 97% rename from src/__tests__/reactive.spec.ts.ts rename to src/__tests__/reactive.spec.ts index 4698815..072f551 100644 --- a/src/__tests__/reactive.spec.ts.ts +++ b/src/__tests__/reactive.spec.ts @@ -301,4 +301,11 @@ describe('reactive', () => { await nextTick() expect(callback.mock.calls[2][0]).toBe(500) }) + + it('should preserve argument count for splice', () => { + const data = reactive([1, 2, 3, 4, 5]) + const tail = data.splice(2) + expect(data).toEqual([1, 2]) + expect(tail).toEqual([3, 4, 5]) + }) }) diff --git a/src/reactive.ts b/src/reactive.ts index 17cd166..e03a197 100644 --- a/src/reactive.ts +++ b/src/reactive.ts @@ -325,8 +325,14 @@ function arrayOperation( case 'fill': return (...args: any[]) => synthetic(...args.map((arg) => r(arg))) case 'splice': - return (start: number, remove?: number, ...inserts: any[]) => - synthetic(start, remove, ...inserts.map((arg) => r(arg))) + return function (start: number, remove?: number, ...inserts: any[]) { + // Preserve the argument count when there's only one argument, + // because if a second argument is passed but undefined, + // it gets treated as 0. + return arguments.length === 1 ? + synthetic(start) : + synthetic(start, remove, ...inserts.map((arg) => r(arg))) + } default: return native }