]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): `$watch` callback parameters type (#6136)
authorwebfansplz <308241863@qq.com>
Wed, 26 Oct 2022 09:43:42 +0000 (17:43 +0800)
committerGitHub <noreply@github.com>
Wed, 26 Oct 2022 09:43:42 +0000 (05:43 -0400)
fix #6135

packages/runtime-core/src/componentPublicInstance.ts
test-dts/watch.test-d.ts

index 1d913673123cb7d8a019be7eefc7bd8e6e1c3b23..1e4c83c96d89bf1c5197587ce8f73839e267ad16 100644 (file)
@@ -193,9 +193,11 @@ export type ComponentPublicInstance<
   $options: Options & MergedComponentOptionsOverride
   $forceUpdate: () => void
   $nextTick: typeof nextTick
-  $watch(
-    source: string | Function,
-    cb: Function,
+  $watch<T extends string | ((...args: any) => any)>(
+    source: T,
+    cb: T extends (...args: any) => infer R
+      ? (...args: [R, R]) => any
+      : (...args: any) => any,
     options?: WatchOptions
   ): WatchStopHandle
 } & P &
index 52d26c93c4c51ed170cbddc7331d60f9baf263e3..2c16cabd9a4d3d4d2f91f74a62b78c00309b85ac 100644 (file)
@@ -1,4 +1,4 @@
-import { ref, computed, watch, expectType } from './index'
+import { ref, computed, watch, expectType, defineComponent } from './index'
 
 const source = ref('foo')
 const source2 = computed(() => source.value)
@@ -75,3 +75,19 @@ watch([someRef, otherRef], values => {
   // no type error
   console.log(value2.a)
 })
+
+// #6135
+defineComponent({
+  data() {
+    return { a: 1 }
+  },
+  created() {
+    this.$watch(
+      () => this.a,
+      (v, ov) => {
+        expectType<number>(v)
+        expectType<number>(ov)
+      }
+    )
+  }
+})