types: improve type 'ReactiveEffect' with generics (#99)

This commit is contained in:
扩散性百万甜面包 2019-10-09 01:48:13 +08:00 committed by Evan You
parent 8da5b007b1
commit 80e5b16f99
2 changed files with 12 additions and 12 deletions

View File

@ -2,11 +2,11 @@ import { OperationTypes } from './operations'
import { Dep, targetMap } from './reactive'
import { EMPTY_OBJ, extend } from '@vue/shared'
export interface ReactiveEffect {
(): any
export interface ReactiveEffect<T = any> {
(): T
isEffect: true
active: boolean
raw: Function
raw: () => T
deps: Array<Dep>
computed?: boolean
scheduler?: (run: Function) => void
@ -35,10 +35,10 @@ export const activeReactiveEffectStack: ReactiveEffect[] = []
export const ITERATE_KEY = Symbol('iterate')
export function effect(
fn: Function,
export function effect<T = any>(
fn: () => T,
options: ReactiveEffectOptions = EMPTY_OBJ
): ReactiveEffect {
): ReactiveEffect<T> {
if ((fn as ReactiveEffect).isEffect) {
fn = (fn as ReactiveEffect).raw
}
@ -59,13 +59,13 @@ export function stop(effect: ReactiveEffect) {
}
}
function createReactiveEffect(
fn: Function,
function createReactiveEffect<T = any>(
fn: () => T,
options: ReactiveEffectOptions
): ReactiveEffect {
const effect = function effect(...args): any {
): ReactiveEffect<T> {
const effect: ReactiveEffect = function effect(...args: any[]): any {
return run(effect as ReactiveEffect, fn, args)
} as ReactiveEffect
}
effect.isEffect = true
effect.active = true
effect.raw = fn

View File

@ -91,7 +91,7 @@ function doWatch(
const instance = currentInstance
const suspense = currentSuspense
let getter: Function
let getter: () => any
if (isArray(source)) {
getter = () =>
source.map(