fix(watch): type inference for computed refs

This commit is contained in:
Evan You 2019-10-14 12:15:09 -04:00
parent 6bd26636c3
commit 6b3ad95fa4
2 changed files with 14 additions and 1 deletions

View File

@ -34,6 +34,9 @@ describe('api: watch', () => {
() => state.count, () => state.count,
(count, prevCount) => { (count, prevCount) => {
dummy = [count, prevCount] dummy = [count, prevCount]
// assert types
count + 1
prevCount + 1
} }
) )
await nextTick() await nextTick()
@ -49,6 +52,9 @@ describe('api: watch', () => {
let dummy let dummy
watch(count, (count, prevCount) => { watch(count, (count, prevCount) => {
dummy = [count, prevCount] dummy = [count, prevCount]
// assert types
count + 1
prevCount + 1
}) })
await nextTick() await nextTick()
expect(dummy).toMatchObject([0, undefined]) expect(dummy).toMatchObject([0, undefined])
@ -64,6 +70,9 @@ describe('api: watch', () => {
let dummy let dummy
watch(plus, (count, prevCount) => { watch(plus, (count, prevCount) => {
dummy = [count, prevCount] dummy = [count, prevCount]
// assert types
count + 1
prevCount + 1
}) })
await nextTick() await nextTick()
expect(dummy).toMatchObject([1, undefined]) expect(dummy).toMatchObject([1, undefined])
@ -81,6 +90,9 @@ describe('api: watch', () => {
let dummy let dummy
watch([() => state.count, count, plus], (vals, oldVals) => { watch([() => state.count, count, plus], (vals, oldVals) => {
dummy = [vals, oldVals] dummy = [vals, oldVals]
// assert types
vals.concat(1)
oldVals.concat(1)
}) })
await nextTick() await nextTick()
expect(dummy).toMatchObject([[1, 1, 2], []]) expect(dummy).toMatchObject([[1, 1, 2], []])

View File

@ -3,6 +3,7 @@ import {
stop, stop,
isRef, isRef,
Ref, Ref,
ComputedRef,
ReactiveEffectOptions ReactiveEffectOptions
} from '@vue/reactivity' } from '@vue/reactivity'
import { queueJob } from './scheduler' import { queueJob } from './scheduler'
@ -32,7 +33,7 @@ export interface WatchOptions {
type StopHandle = () => void type StopHandle = () => void
type WatcherSource<T = any> = Ref<T> | (() => T) type WatcherSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
type MapSources<T> = { type MapSources<T> = {
[K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never [K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never