types(watch): allow readonly arrays for watching multiple sources (#281)

This commit is contained in:
Adam Lewkowicz 2019-10-15 22:44:14 +02:00 committed by Evan You
parent 0b2573f3d1
commit 555e3be69d
2 changed files with 23 additions and 1 deletions

View File

@ -103,6 +103,28 @@ describe('api: watch', () => {
expect(dummy).toMatchObject([[2, 2, 3], [1, 1, 2]])
})
it('watching multiple sources: readonly array', async () => {
const state = reactive({ count: 1 })
const status = ref(false)
let dummy
watch([() => state.count, status] as const, (vals, oldVals) => {
dummy = [vals, oldVals]
let [count] = vals
let [, oldStatus] = oldVals
// assert types
count + 1
oldStatus === true
})
await nextTick()
expect(dummy).toMatchObject([[1, false], []])
state.count++
status.value = false
await nextTick()
expect(dummy).toMatchObject([[2, false], [1, false]])
})
it('stopping the watcher', async () => {
const state = reactive({ count: 0 })
let dummy

View File

@ -56,7 +56,7 @@ export function watch<T>(
): StopHandle
// overload #3: array of multiple sources + cb
export function watch<T extends WatcherSource<unknown>[]>(
export function watch<T extends readonly WatcherSource<unknown>[]>(
sources: T,
cb: (
newValues: MapSources<T>,