From 016231d09f1abc99b0654b8454d2e9a969251e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=A9=E6=95=A3=E6=80=A7=E7=99=BE=E4=B8=87=E7=94=9C?= =?UTF-8?q?=E9=9D=A2=E5=8C=85?= Date: Tue, 8 Oct 2019 22:48:24 +0800 Subject: [PATCH] types: improve type for WatchHandler (#160) --- packages/runtime-core/src/apiOptions.ts | 8 ++++---- packages/runtime-core/src/apiWatch.ts | 14 +++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/packages/runtime-core/src/apiOptions.ts b/packages/runtime-core/src/apiOptions.ts index 14276eabf..87902f7f8 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 b7139d62e..5371a8f59 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)) {