From 13263d33b98faae40e9e071e4861c2964fb1c211 Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Sat, 18 Jun 2022 14:20:10 +0800 Subject: [PATCH 1/2] fix(type): `$watch` callback parameters type --- packages/runtime-core/src/componentPublicInstance.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 1d913673123..4ec2eaf78f8 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 + : Function, options?: WatchOptions ): WatchStopHandle } & P & From 12f38aae6370126f7034659978369a10f2402dc8 Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Sat, 18 Jun 2022 14:33:28 +0800 Subject: [PATCH 2/2] chore: update --- .../src/componentPublicInstance.ts | 2 +- test-dts/watch.test-d.ts | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 4ec2eaf78f8..1e4c83c96d8 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -197,7 +197,7 @@ export type ComponentPublicInstance< source: T, cb: T extends (...args: any) => infer R ? (...args: [R, R]) => any - : Function, + : (...args: any) => any, options?: WatchOptions ): WatchStopHandle } & P & diff --git a/test-dts/watch.test-d.ts b/test-dts/watch.test-d.ts index 52d26c93c4c..2c16cabd9a4 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) + } + ) + } +})