]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): fix defineModel watch type error (#9942)
author丶远方 <yangpanteng@gmail.com>
Sat, 30 Dec 2023 00:22:56 +0000 (08:22 +0800)
committerGitHub <noreply@github.com>
Sat, 30 Dec 2023 00:22:56 +0000 (08:22 +0800)
close #9939

packages/dts-test/watch.test-d.ts
packages/runtime-core/src/apiWatch.ts

index 323716d8a05c06caf919338dd8e9b0a14c195426..5986d3d30b182fb3bab939341d809f4a9d26f6c8 100644 (file)
@@ -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<Steps>(value)
   })
 }
+
+{
+  // defineModel
+  const bool = defineModel({ default: false })
+  watch(bool, value => {
+    expectType<boolean>(value)
+  })
+
+  const bool1 = defineModel<boolean>()
+  watch(bool1, value => {
+    expectType<boolean | undefined>(value)
+  })
+
+  const msg = defineModel<string>({ required: true })
+  watch(msg, value => {
+    expectType<string>(value)
+  })
+
+  const arr = defineModel<string[]>({ required: true })
+  watch(arr, value => {
+    expectType<string[]>(value)
+  })
+
+  const obj = defineModel<{ foo: string }>({ required: true })
+  watch(obj, value => {
+    expectType<{ foo: string }>(value)
+  })
+}
index ecd750117db1928d23d14211a6165fe55783d43f..2f0364388b9f6e58023b98dd83a50c40e6a1af44 100644 (file)
@@ -115,6 +115,13 @@ const INITIAL_WATCHER_VALUE = {}
 
 type MultiWatchSources = (WatchSource<unknown> | object)[]
 
+// overload: single source + cb
+export function watch<T, Immediate extends Readonly<boolean> = false>(
+  source: WatchSource<T>,
+  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
+  options?: WatchOptions<Immediate>,
+): WatchStopHandle
+
 // overload: array of multiple sources + cb
 export function watch<
   T extends MultiWatchSources,
@@ -137,13 +144,6 @@ export function watch<
   options?: WatchOptions<Immediate>,
 ): WatchStopHandle
 
-// overload: single source + cb
-export function watch<T, Immediate extends Readonly<boolean> = false>(
-  source: WatchSource<T>,
-  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
-  options?: WatchOptions<Immediate>,
-): WatchStopHandle
-
 // overload: watching reactive object w/ cb
 export function watch<
   T extends object,