From: Adam Lewkowicz <37848894+alk831@users.noreply.github.com> Date: Tue, 15 Oct 2019 20:44:14 +0000 (+0200) Subject: types(watch): allow readonly arrays for watching multiple sources (#281) X-Git-Tag: v3.0.0-alpha.0~420 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=555e3be69d39f4b35a312916253d9f38cbcab495;p=thirdparty%2Fvuejs%2Fcore.git types(watch): allow readonly arrays for watching multiple sources (#281) --- diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 4ab13ddbe3..5f4dd323b1 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 50901a76da..2127a4eea6 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,