@@ -44,13 +44,17 @@ export type WatchCallback<V = any, OV = any> = (
44
44
) => any
45
45
46
46
type MapSources < T > = {
47
- [ K in keyof T ] : T [ K ] extends WatchSource < infer V > ? V : never
47
+ [ K in keyof T ] : T [ K ] extends WatchSource < infer V >
48
+ ? V
49
+ : T [ K ] extends object ? T [ K ] : never
48
50
}
49
51
50
52
type MapOldSources < T , Immediate > = {
51
53
[ K in keyof T ] : T [ K ] extends WatchSource < infer V >
52
54
? Immediate extends true ? ( V | undefined ) : V
53
- : never
55
+ : T [ K ] extends object
56
+ ? Immediate extends true ? ( T [ K ] | undefined ) : T [ K ]
57
+ : never
54
58
}
55
59
56
60
type InvalidateCbRegistrator = ( cb : ( ) => void ) => void
@@ -86,7 +90,7 @@ const INITIAL_WATCHER_VALUE = {}
86
90
// on position in the source array. Otherwise the values will get a union type
87
91
// of all possible value types.
88
92
export function watch <
89
- T extends Readonly < WatchSource < unknown > [ ] > ,
93
+ T extends Readonly < Array < WatchSource < unknown > | object > > ,
90
94
Immediate extends Readonly < boolean > = false
91
95
> (
92
96
sources : T ,
@@ -147,17 +151,31 @@ function doWatch(
147
151
}
148
152
}
149
153
154
+ const warnInvalidSource = ( s : unknown ) => {
155
+ warn (
156
+ `Invalid watch source: ` ,
157
+ s ,
158
+ `A watch source can only be a getter/effect function, a ref, ` +
159
+ `a reactive object, or an array of these types.`
160
+ )
161
+ }
162
+
150
163
const instance = currentInstance
151
164
152
165
let getter : ( ) => any
153
166
if ( isArray ( source ) ) {
154
167
getter = ( ) =>
155
- source . map (
156
- s =>
157
- isRef ( s )
158
- ? s . value
159
- : callWithErrorHandling ( s , instance , ErrorCodes . WATCH_GETTER )
160
- )
168
+ source . map ( s => {
169
+ if ( isRef ( s ) ) {
170
+ return s . value
171
+ } else if ( isReactive ( s ) ) {
172
+ return traverse ( s )
173
+ } else if ( isFunction ( s ) ) {
174
+ return callWithErrorHandling ( s , instance , ErrorCodes . WATCH_GETTER )
175
+ } else {
176
+ __DEV__ && warnInvalidSource ( s )
177
+ }
178
+ } )
161
179
} else if ( isRef ( source ) ) {
162
180
getter = ( ) => source . value
163
181
} else if ( isReactive ( source ) ) {
@@ -187,13 +205,7 @@ function doWatch(
187
205
}
188
206
} else {
189
207
getter = NOOP
190
- __DEV__ &&
191
- warn (
192
- `Invalid watch source: ` ,
193
- source ,
194
- `A watch source can only be a getter/effect function, a ref, ` +
195
- `a reactive object, or an array of these types.`
196
- )
208
+ __DEV__ && warnInvalidSource ( source )
197
209
}
198
210
199
211
if ( cb && deep ) {
0 commit comments