@@ -32,18 +32,16 @@ export type WatchCallback<V = any, OV = any> = (
32
32
onInvalidate : InvalidateCbRegistrator
33
33
) => any
34
34
35
- type MapSources < T > = {
36
- [ K in keyof T ] : T [ K ] extends WatchSource < infer V > ? V : never
37
- }
38
-
39
- type MapOldSources < T , Immediate > = {
35
+ declare type MapSources < T , Immediate > = {
40
36
[ K in keyof T ] : T [ K ] extends WatchSource < infer V >
41
37
? Immediate extends true
42
38
? V | undefined
43
39
: V
44
40
: never
45
41
}
46
42
43
+ type MultiWatchSources = ( WatchSource < unknown > | object ) [ ]
44
+
47
45
export interface WatchOptionsBase {
48
46
flush ?: FlushMode
49
47
// onTrack?: ReactiveEffectOptions['onTrack'];
@@ -204,8 +202,8 @@ function patchWatcherTeardown(watcher: VueWatcher, runCleanup: () => void) {
204
202
205
203
function createWatcher (
206
204
vm : ComponentInstance ,
207
- source : WatchSource < unknown > | WatchSource < unknown > [ ] | WatchEffect ,
208
- cb : WatchCallback < any > | null ,
205
+ source : WatchSource | WatchSource [ ] | WatchEffect ,
206
+ cb : WatchCallback | null ,
209
207
options : WatchOptions
210
208
) : ( ) => void {
211
209
if ( __DEV__ && ! cb ) {
@@ -416,27 +414,47 @@ export function watchSyncEffect(effect: WatchEffect) {
416
414
return watchEffect ( effect , { flush : 'sync' } )
417
415
}
418
416
419
- // overload #1: array of multiple sources + cb
420
- // Readonly constraint helps the callback to correctly infer value types based
421
- // on position in the source array. Otherwise the values will get a union type
422
- // of all possible value types.
417
+ // overload #1 + #2 + #3: array of multiple sources + cb
418
+
419
+ // overload #1: In readonly case the first overload must be spread tuple type.
420
+ // In otherwise members in tuple can not get the correct types.
421
+ export function watch <
422
+ T extends Readonly < MultiWatchSources > ,
423
+ Immediate extends Readonly < boolean > = false
424
+ > (
425
+ sources : [ ...T ] ,
426
+ cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
427
+ options ?: WatchOptions < Immediate >
428
+ ) : WatchStopHandle
429
+
430
+ // overload #2: for not spread readonly tuple
431
+ export function watch <
432
+ T extends Readonly < MultiWatchSources > ,
433
+ Immediate extends Readonly < boolean > = false
434
+ > (
435
+ sources : T ,
436
+ cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
437
+ options ?: WatchOptions < Immediate >
438
+ ) : WatchStopHandle
439
+
440
+ // overload #3: for not readonly multiSources
423
441
export function watch <
424
- T extends Readonly < WatchSource < unknown > [ ] > ,
442
+ T extends MultiWatchSources ,
425
443
Immediate extends Readonly < boolean > = false
426
444
> (
427
445
sources : [ ...T ] ,
428
- cb : WatchCallback < MapSources < T > , MapOldSources < T , Immediate > > ,
446
+ cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
429
447
options ?: WatchOptions < Immediate >
430
448
) : WatchStopHandle
431
449
432
- // overload #2 : single source + cb
450
+ // overload #4 : single source + cb
433
451
export function watch < T , Immediate extends Readonly < boolean > = false > (
434
452
source : WatchSource < T > ,
435
453
cb : WatchCallback < T , Immediate extends true ? T | undefined : T > ,
436
454
options ?: WatchOptions < Immediate >
437
455
) : WatchStopHandle
438
456
439
- // overload #3 : watching reactive object w/ cb
457
+ // overload #5 : watching reactive object w/ cb
440
458
export function watch <
441
459
T extends object ,
442
460
Immediate extends Readonly < boolean > = false
0 commit comments