types(watch): allow readonly arrays for watching multiple sources (#281)
This commit is contained in:
parent
0b2573f3d1
commit
555e3be69d
@ -103,6 +103,28 @@ describe('api: watch', () => {
|
|||||||
expect(dummy).toMatchObject([[2, 2, 3], [1, 1, 2]])
|
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 () => {
|
it('stopping the watcher', async () => {
|
||||||
const state = reactive({ count: 0 })
|
const state = reactive({ count: 0 })
|
||||||
let dummy
|
let dummy
|
||||||
|
@ -56,7 +56,7 @@ export function watch<T>(
|
|||||||
): StopHandle
|
): StopHandle
|
||||||
|
|
||||||
// overload #3: array of multiple sources + cb
|
// overload #3: array of multiple sources + cb
|
||||||
export function watch<T extends WatcherSource<unknown>[]>(
|
export function watch<T extends readonly WatcherSource<unknown>[]>(
|
||||||
sources: T,
|
sources: T,
|
||||||
cb: (
|
cb: (
|
||||||
newValues: MapSources<T>,
|
newValues: MapSources<T>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user