]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(watch): allow readonly arrays for watching multiple sources (#281)
authorAdam Lewkowicz <37848894+alk831@users.noreply.github.com>
Tue, 15 Oct 2019 20:44:14 +0000 (22:44 +0200)
committerEvan You <yyx990803@gmail.com>
Tue, 15 Oct 2019 20:44:14 +0000 (16:44 -0400)
packages/runtime-core/__tests__/apiWatch.spec.ts
packages/runtime-core/src/apiWatch.ts

index 4ab13ddbe34d8dfbe650d118bac8d3953f53dab5..5f4dd323b163fb85d8f47853066b76c4d6acabc0 100644 (file)
@@ -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
index 50901a76daa3957f6ec18aac463f55c932358a41..2127a4eea670483bd21de3e64247b21919924933 100644 (file)
@@ -56,7 +56,7 @@ export function watch<T>(
 ): StopHandle
 
 // overload #3: array of multiple sources + cb
-export function watch<T extends WatcherSource<unknown>[]>(
+export function watch<T extends readonly WatcherSource<unknown>[]>(
   sources: T,
   cb: (
     newValues: MapSources<T>,