From 6b3ad95fa4bdbea99f2f96404cc0b35f4a691595 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 14 Oct 2019 12:15:09 -0400 Subject: [PATCH] fix(watch): type inference for computed refs --- packages/runtime-core/__tests__/apiWatch.spec.ts | 12 ++++++++++++ packages/runtime-core/src/apiWatch.ts | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 9af703499e..4ab13ddbe3 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -34,6 +34,9 @@ describe('api: watch', () => { () => state.count, (count, prevCount) => { dummy = [count, prevCount] + // assert types + count + 1 + prevCount + 1 } ) await nextTick() @@ -49,6 +52,9 @@ describe('api: watch', () => { let dummy watch(count, (count, prevCount) => { dummy = [count, prevCount] + // assert types + count + 1 + prevCount + 1 }) await nextTick() expect(dummy).toMatchObject([0, undefined]) @@ -64,6 +70,9 @@ describe('api: watch', () => { let dummy watch(plus, (count, prevCount) => { dummy = [count, prevCount] + // assert types + count + 1 + prevCount + 1 }) await nextTick() expect(dummy).toMatchObject([1, undefined]) @@ -81,6 +90,9 @@ describe('api: watch', () => { let dummy watch([() => state.count, count, plus], (vals, oldVals) => { dummy = [vals, oldVals] + // assert types + vals.concat(1) + oldVals.concat(1) }) await nextTick() expect(dummy).toMatchObject([[1, 1, 2], []]) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 927db33321..50901a76da 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -3,6 +3,7 @@ import { stop, isRef, Ref, + ComputedRef, ReactiveEffectOptions } from '@vue/reactivity' import { queueJob } from './scheduler' @@ -32,7 +33,7 @@ export interface WatchOptions { type StopHandle = () => void -type WatcherSource = Ref | (() => T) +type WatcherSource = Ref | ComputedRef | (() => T) type MapSources = { [K in keyof T]: T[K] extends WatcherSource ? V : never -- 2.47.3