From: 丶远方 Date: Sat, 30 Dec 2023 00:22:56 +0000 (+0800) Subject: fix(types): fix defineModel watch type error (#9942) X-Git-Tag: v3.4.1~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4af85835f7e593a7dffa7dc7e99f14877eb70fd1;p=thirdparty%2Fvuejs%2Fcore.git fix(types): fix defineModel watch type error (#9942) close #9939 --- diff --git a/packages/dts-test/watch.test-d.ts b/packages/dts-test/watch.test-d.ts index 323716d8a0..5986d3d30b 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 ecd750117d..2f0364388b 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,