]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): type inference for computed refs
authorEvan You <yyx990803@gmail.com>
Mon, 14 Oct 2019 16:15:09 +0000 (12:15 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 14 Oct 2019 16:15:09 +0000 (12:15 -0400)
packages/runtime-core/__tests__/apiWatch.spec.ts
packages/runtime-core/src/apiWatch.ts

index 9af703499e2abdc9c4937e5ee6a5c307f2420e4c..4ab13ddbe34d8dfbe650d118bac8d3953f53dab5 100644 (file)
@@ -34,6 +34,9 @@ describe('api: watch', () => {
       () => state.count,
       (count, prevCount) => {
         dummy = [count, prevCount]
+        // assert types
+        count + 1
+        prevCount + 1
       }
     )
     await nextTick()
@@ -49,6 +52,9 @@ describe('api: watch', () => {
     let dummy
     watch(count, (count, prevCount) => {
       dummy = [count, prevCount]
+      // assert types
+      count + 1
+      prevCount + 1
     })
     await nextTick()
     expect(dummy).toMatchObject([0, undefined])
@@ -64,6 +70,9 @@ describe('api: watch', () => {
     let dummy
     watch(plus, (count, prevCount) => {
       dummy = [count, prevCount]
+      // assert types
+      count + 1
+      prevCount + 1
     })
     await nextTick()
     expect(dummy).toMatchObject([1, undefined])
@@ -81,6 +90,9 @@ describe('api: watch', () => {
     let dummy
     watch([() => state.count, count, plus], (vals, oldVals) => {
       dummy = [vals, oldVals]
+      // assert types
+      vals.concat(1)
+      oldVals.concat(1)
     })
     await nextTick()
     expect(dummy).toMatchObject([[1, 1, 2], []])
index 927db3332125c3e5017b9eb035c6a8fe9a2f7f25..50901a76daa3957f6ec18aac463f55c932358a41 100644 (file)
@@ -3,6 +3,7 @@ import {
   stop,
   isRef,
   Ref,
+  ComputedRef,
   ReactiveEffectOptions
 } from '@vue/reactivity'
 import { queueJob } from './scheduler'
@@ -32,7 +33,7 @@ export interface WatchOptions {
 
 type StopHandle = () => void
 
-type WatcherSource<T = any> = Ref<T> | (() => T)
+type WatcherSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
 
 type MapSources<T> = {
   [K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never