From 4af85835f7e593a7dffa7dc7e99f14877eb70fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Sat, 30 Dec 2023 08:22:56 +0800 Subject: [PATCH] fix(types): fix defineModel watch type error (#9942) close #9939 --- packages/dts-test/watch.test-d.ts | 37 ++++++++++++++++++++++++++- packages/runtime-core/src/apiWatch.ts | 14 +++++----- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/dts-test/watch.test-d.ts b/packages/dts-test/watch.test-d.ts index 323716d8a05..5986d3d30b1 100644 --- a/packages/dts-test/watch.test-d.ts +++ b/packages/dts-test/watch.test-d.ts @@ -1,4 +1,11 @@ -import { computed, defineComponent, ref, shallowRef, watch } from 'vue' +import { + computed, + defineComponent, + defineModel, + ref, + shallowRef, + watch, +} from 'vue' import { expectType } from './utils' const source = ref('foo') @@ -106,3 +113,31 @@ defineComponent({ expectType(value) }) } + +{ + // defineModel + const bool = defineModel({ default: false }) + watch(bool, value => { + expectType(value) + }) + + const bool1 = defineModel() + watch(bool1, value => { + expectType(value) + }) + + const msg = defineModel({ required: true }) + watch(msg, value => { + expectType(value) + }) + + const arr = defineModel({ required: true }) + watch(arr, value => { + expectType(value) + }) + + const obj = defineModel<{ foo: string }>({ required: true }) + watch(obj, value => { + expectType<{ foo: string }>(value) + }) +} diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index ecd750117db..2f0364388b9 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -115,6 +115,13 @@ const INITIAL_WATCHER_VALUE = {} type MultiWatchSources = (WatchSource | object)[] +// overload: single source + cb +export function watch = false>( + source: WatchSource, + cb: WatchCallback, + options?: WatchOptions, +): WatchStopHandle + // overload: array of multiple sources + cb export function watch< T extends MultiWatchSources, @@ -137,13 +144,6 @@ export function watch< options?: WatchOptions, ): WatchStopHandle -// overload: single source + cb -export function watch = false>( - source: WatchSource, - cb: WatchCallback, - options?: WatchOptions, -): WatchStopHandle - // overload: watching reactive object w/ cb export function watch< T extends object,