onInvalidate: InvalidateCbRegistrator
) => any
-type MapSources<T> = {
- [K in keyof T]: T[K] extends WatchSource<infer V>
- ? V
- : T[K] extends object ? T[K] : never
-}
-
-type MapOldSources<T, Immediate> = {
+type MapSources<T, Immediate> = {
[K in keyof T]: T[K] extends WatchSource<infer V>
? Immediate extends true ? (V | undefined) : V
: T[K] extends object
Immediate extends Readonly<boolean> = false
>(
sources: T,
- cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
+ cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate>
): WatchStopHandle
): WatchStopHandle
// implementation
-export function watch<T = any>(
- source: WatchSource<T> | WatchSource<T>[],
- cb: WatchCallback<T>,
- options?: WatchOptions
+export function watch<T = any, Immediate extends Readonly<boolean> = false>(
+ source: T | WatchSource<T>,
+ cb: any,
+ options?: WatchOptions<Immediate>
): WatchStopHandle {
if (__DEV__ && !isFunction(cb)) {
warn(
`supports \`watch(source, cb, options?) signature.`
)
}
- return doWatch(source, cb, options)
+ return doWatch(source as any, cb, options)
}
function doWatch(
- source: WatchSource | WatchSource[] | WatchEffect,
+ source: WatchSource | WatchSource[] | WatchEffect | object,
cb: WatchCallback | null,
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ,
instance = currentInstance
}
}
- // important: mark the job as a watcher callback so that scheduler knows it
+ // important: mark the job as a watcher callback so that scheduler knows
// it is allowed to self-trigger (#1727)
job.allowRecurse = !!cb
- let scheduler: (job: () => any) => void
+ let scheduler: ReactiveEffectOptions['scheduler']
if (flush === 'sync') {
scheduler = job
} else if (flush === 'post') {
export function instanceWatch(
this: ComponentInternalInstance,
source: string | Function,
- cb: Function,
+ cb: WatchCallback,
options?: WatchOptions
): WatchStopHandle {
const publicThis = this.proxy as any