From: webfansplz <308241863@qq.com> Date: Wed, 26 Oct 2022 09:43:42 +0000 (+0800) Subject: fix(types): `$watch` callback parameters type (#6136) X-Git-Tag: v3.2.42~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41d9c47300888fce9d4ff6a02f69d8a912cded8f;p=thirdparty%2Fvuejs%2Fcore.git fix(types): `$watch` callback parameters type (#6136) fix #6135 --- diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 1d91367312..1e4c83c96d 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -193,9 +193,11 @@ export type ComponentPublicInstance< $options: Options & MergedComponentOptionsOverride $forceUpdate: () => void $nextTick: typeof nextTick - $watch( - source: string | Function, - cb: Function, + $watch any)>( + source: T, + cb: T extends (...args: any) => infer R + ? (...args: [R, R]) => any + : (...args: any) => any, options?: WatchOptions ): WatchStopHandle } & P & diff --git a/test-dts/watch.test-d.ts b/test-dts/watch.test-d.ts index 52d26c93c4..2c16cabd9a 100644 --- a/test-dts/watch.test-d.ts +++ b/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) @@ -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(v) + expectType(ov) + } + ) + } +})