]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: improve type for WatchHandler (#160)
author扩散性百万甜面包 <himself65@outlook.com>
Tue, 8 Oct 2019 14:48:24 +0000 (22:48 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 8 Oct 2019 14:48:24 +0000 (10:48 -0400)
packages/runtime-core/src/apiOptions.ts
packages/runtime-core/src/apiWatch.ts

index 14276eabf18ce2eedfce69c64975e15854816e70..87902f7f81b09505f1e7ec2e32fdd99501ce61d3 100644 (file)
@@ -116,11 +116,11 @@ export type ExtractComputedReturns<T extends any> = {
     : ReturnType<T[key]>
 }
 
-type WatchHandler = (
-  val: any,
-  oldVal: any,
+export type WatchHandler<T = any> = (
+  val: T,
+  oldVal: T,
   onCleanup: CleanupRegistrator
-) => void
+) => any
 
 type ComponentWatchOptions = Record<
   string,
index b7139d62ea67eb72eeb6df21d600b8aec734d057..5371a8f59a4e02c03aacc37239864d380587eb3f 100644 (file)
@@ -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<T>(
   source: WatcherSource<T>,
-  cb: (newValue: T, oldValue: T, onCleanup: CleanupRegistrator) => any,
+  cb: WatchHandler<T>,
   options?: WatchOptions
 ): StopHandle
 
@@ -65,14 +66,9 @@ export function watch<T extends WatcherSource<unknown>[]>(
 ): StopHandle
 
 // implementation
-export function watch(
-  effectOrSource:
-    | WatcherSource<unknown>
-    | WatcherSource<unknown>[]
-    | SimpleEffect,
-  cbOrOptions?:
-    | ((value: any, oldValue: any, onCleanup: CleanupRegistrator) => any)
-    | WatchOptions,
+export function watch<T = any>(
+  effectOrSource: WatcherSource<T> | WatcherSource<T>[] | SimpleEffect,
+  cbOrOptions?: WatchHandler<T> | WatchOptions,
   options?: WatchOptions
 ): StopHandle {
   if (isFunction(cbOrOptions)) {