Skip to content

Commit 4af8583

Browse files
fix(types): fix defineModel watch type error (#9942)
close #9939
1 parent a41c5f1 commit 4af8583

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

Diff for: packages/dts-test/watch.test-d.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { computed, defineComponent, ref, shallowRef, watch } from 'vue'
1+
import {
2+
computed,
3+
defineComponent,
4+
defineModel,
5+
ref,
6+
shallowRef,
7+
watch,
8+
} from 'vue'
29
import { expectType } from './utils'
310

411
const source = ref('foo')
@@ -106,3 +113,31 @@ defineComponent({
106113
expectType<Steps>(value)
107114
})
108115
}
116+
117+
{
118+
// defineModel
119+
const bool = defineModel({ default: false })
120+
watch(bool, value => {
121+
expectType<boolean>(value)
122+
})
123+
124+
const bool1 = defineModel<boolean>()
125+
watch(bool1, value => {
126+
expectType<boolean | undefined>(value)
127+
})
128+
129+
const msg = defineModel<string>({ required: true })
130+
watch(msg, value => {
131+
expectType<string>(value)
132+
})
133+
134+
const arr = defineModel<string[]>({ required: true })
135+
watch(arr, value => {
136+
expectType<string[]>(value)
137+
})
138+
139+
const obj = defineModel<{ foo: string }>({ required: true })
140+
watch(obj, value => {
141+
expectType<{ foo: string }>(value)
142+
})
143+
}

Diff for: packages/runtime-core/src/apiWatch.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ const INITIAL_WATCHER_VALUE = {}
115115

116116
type MultiWatchSources = (WatchSource<unknown> | object)[]
117117

118+
// overload: single source + cb
119+
export function watch<T, Immediate extends Readonly<boolean> = false>(
120+
source: WatchSource<T>,
121+
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
122+
options?: WatchOptions<Immediate>,
123+
): WatchStopHandle
124+
118125
// overload: array of multiple sources + cb
119126
export function watch<
120127
T extends MultiWatchSources,
@@ -137,13 +144,6 @@ export function watch<
137144
options?: WatchOptions<Immediate>,
138145
): WatchStopHandle
139146

140-
// overload: single source + cb
141-
export function watch<T, Immediate extends Readonly<boolean> = false>(
142-
source: WatchSource<T>,
143-
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
144-
options?: WatchOptions<Immediate>,
145-
): WatchStopHandle
146-
147147
// overload: watching reactive object w/ cb
148148
export function watch<
149149
T extends object,

0 commit comments

Comments
 (0)