From: 扩散性百万甜面包 Date: Tue, 8 Oct 2019 14:48:24 +0000 (+0800) Subject: types: improve type for WatchHandler (#160) X-Git-Tag: v3.0.0-alpha.0~562 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=016231d09f1abc99b0654b8454d2e9a969251e17;p=thirdparty%2Fvuejs%2Fcore.git types: improve type for WatchHandler (#160) --- diff --git a/packages/runtime-core/src/apiOptions.ts b/packages/runtime-core/src/apiOptions.ts index 14276eabf1..87902f7f81 100644 --- a/packages/runtime-core/src/apiOptions.ts +++ b/packages/runtime-core/src/apiOptions.ts @@ -116,11 +116,11 @@ export type ExtractComputedReturns = { : ReturnType } -type WatchHandler = ( - val: any, - oldVal: any, +export type WatchHandler = ( + val: T, + oldVal: T, onCleanup: CleanupRegistrator -) => void +) => any type ComponentWatchOptions = Record< string, diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index b7139d62ea..5371a8f59a 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -20,6 +20,7 @@ import { } from './errorHandling' import { onBeforeUnmount } from './apiLifecycle' import { queuePostRenderEffect } from './createRenderer' +import { WatchHandler } from './apiOptions' export interface WatchOptions { lazy?: boolean @@ -49,7 +50,7 @@ export function watch(effect: SimpleEffect, options?: WatchOptions): StopHandle // overload #2: single source + cb export function watch( source: WatcherSource, - cb: (newValue: T, oldValue: T, onCleanup: CleanupRegistrator) => any, + cb: WatchHandler, options?: WatchOptions ): StopHandle @@ -65,14 +66,9 @@ export function watch[]>( ): StopHandle // implementation -export function watch( - effectOrSource: - | WatcherSource - | WatcherSource[] - | SimpleEffect, - cbOrOptions?: - | ((value: any, oldValue: any, onCleanup: CleanupRegistrator) => any) - | WatchOptions, +export function watch( + effectOrSource: WatcherSource | WatcherSource[] | SimpleEffect, + cbOrOptions?: WatchHandler | WatchOptions, options?: WatchOptions ): StopHandle { if (isFunction(cbOrOptions)) {