Skip to content

Commit 41d9c47

Browse files
authored
fix(types): $watch callback parameters type (#6136)
fix #6135
1 parent eab7604 commit 41d9c47

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/runtime-core/src/componentPublicInstance.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ export type ComponentPublicInstance<
193193
$options: Options & MergedComponentOptionsOverride
194194
$forceUpdate: () => void
195195
$nextTick: typeof nextTick
196-
$watch(
197-
source: string | Function,
198-
cb: Function,
196+
$watch<T extends string | ((...args: any) => any)>(
197+
source: T,
198+
cb: T extends (...args: any) => infer R
199+
? (...args: [R, R]) => any
200+
: (...args: any) => any,
199201
options?: WatchOptions
200202
): WatchStopHandle
201203
} & P &

test-dts/watch.test-d.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, computed, watch, expectType } from './index'
1+
import { ref, computed, watch, expectType, defineComponent } from './index'
22

33
const source = ref('foo')
44
const source2 = computed(() => source.value)
@@ -75,3 +75,19 @@ watch([someRef, otherRef], values => {
7575
// no type error
7676
console.log(value2.a)
7777
})
78+
79+
// #6135
80+
defineComponent({
81+
data() {
82+
return { a: 1 }
83+
},
84+
created() {
85+
this.$watch(
86+
() => this.a,
87+
(v, ov) => {
88+
expectType<number>(v)
89+
expectType<number>(ov)
90+
}
91+
)
92+
}
93+
})

0 commit comments

Comments
 (0)