@@ -32,18 +32,16 @@ export type WatchCallback<V = any, OV = any> = (
3232 onInvalidate : InvalidateCbRegistrator
3333) => any
3434
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 > = {
4036 [ K in keyof T ] : T [ K ] extends WatchSource < infer V >
4137 ? Immediate extends true
4238 ? V | undefined
4339 : V
4440 : never
4541}
4642
43+ type MultiWatchSources = ( WatchSource < unknown > | object ) [ ]
44+
4745export interface WatchOptionsBase {
4846 flush ?: FlushMode
4947 // onTrack?: ReactiveEffectOptions['onTrack'];
@@ -204,8 +202,8 @@ function patchWatcherTeardown(watcher: VueWatcher, runCleanup: () => void) {
204202
205203function createWatcher (
206204 vm : ComponentInstance ,
207- source : WatchSource < unknown > | WatchSource < unknown > [ ] | WatchEffect ,
208- cb : WatchCallback < any > | null ,
205+ source : WatchSource | WatchSource [ ] | WatchEffect ,
206+ cb : WatchCallback | null ,
209207 options : WatchOptions
210208) : ( ) => void {
211209 if ( __DEV__ && ! cb ) {
@@ -416,27 +414,47 @@ export function watchSyncEffect(effect: WatchEffect) {
416414 return watchEffect ( effect , { flush : 'sync' } )
417415}
418416
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
423441export function watch <
424- T extends Readonly < WatchSource < unknown > [ ] > ,
442+ T extends MultiWatchSources ,
425443 Immediate extends Readonly < boolean > = false
426444> (
427445 sources : [ ...T ] ,
428- cb : WatchCallback < MapSources < T > , MapOldSources < T , Immediate > > ,
446+ cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
429447 options ?: WatchOptions < Immediate >
430448) : WatchStopHandle
431449
432- // overload #2 : single source + cb
450+ // overload #4 : single source + cb
433451export function watch < T , Immediate extends Readonly < boolean > = false > (
434452 source : WatchSource < T > ,
435453 cb : WatchCallback < T , Immediate extends true ? T | undefined : T > ,
436454 options ?: WatchOptions < Immediate >
437455) : WatchStopHandle
438456
439- // overload #3 : watching reactive object w/ cb
457+ // overload #5 : watching reactive object w/ cb
440458export function watch <
441459 T extends object ,
442460 Immediate extends Readonly < boolean > = false
0 commit comments