From 555e3be69d39f4b35a312916253d9f38cbcab495 Mon Sep 17 00:00:00 2001 From: Adam Lewkowicz <37848894+alk831@users.noreply.github.com> Date: Tue, 15 Oct 2019 22:44:14 +0200 Subject: [PATCH] types(watch): allow readonly arrays for watching multiple sources (#281) --- .../runtime-core/__tests__/apiWatch.spec.ts | 22 +++++++++++++++++++ packages/runtime-core/src/apiWatch.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 4ab13ddb..5f4dd323 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -103,6 +103,28 @@ describe('api: watch', () => { expect(dummy).toMatchObject([[2, 2, 3], [1, 1, 2]]) }) + it('watching multiple sources: readonly array', async () => { + const state = reactive({ count: 1 }) + const status = ref(false) + + let dummy + watch([() => state.count, status] as const, (vals, oldVals) => { + dummy = [vals, oldVals] + let [count] = vals + let [, oldStatus] = oldVals + // assert types + count + 1 + oldStatus === true + }) + await nextTick() + expect(dummy).toMatchObject([[1, false], []]) + + state.count++ + status.value = false + await nextTick() + expect(dummy).toMatchObject([[2, false], [1, false]]) + }) + it('stopping the watcher', async () => { const state = reactive({ count: 0 }) let dummy diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 50901a76..2127a4ee 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -56,7 +56,7 @@ export function watch( ): StopHandle // overload #3: array of multiple sources + cb -export function watch[]>( +export function watch[]>( sources: T, cb: ( newValues: MapSources,