Skip to content

Commit

Permalink
fix(types): $watch callback parameters type (#6136)
Browse files Browse the repository at this point in the history
fix #6135
  • Loading branch information
webfansplz committed Oct 26, 2022
1 parent eab7604 commit 41d9c47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/runtime-core/src/componentPublicInstance.ts
Expand Up @@ -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 &
Expand Down
18 changes: 17 additions & 1 deletion test-dts/watch.test-d.ts
@@ -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)
Expand Down Expand Up @@ -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)
}
)
}
})

0 comments on commit 41d9c47

Please sign in to comment.