2020-06-09 22:17:42 +08:00
|
|
|
import { ref, computed, watch, expectType } from './index'
|
2020-02-14 01:08:21 +08:00
|
|
|
|
|
|
|
const source = ref('foo')
|
|
|
|
const source2 = computed(() => source.value)
|
|
|
|
const source3 = () => 1
|
|
|
|
|
|
|
|
// lazy watcher will have consistent types for oldValue.
|
2020-02-18 12:14:19 +08:00
|
|
|
watch(source, (value, oldValue) => {
|
|
|
|
expectType<string>(value)
|
|
|
|
expectType<string>(oldValue)
|
|
|
|
})
|
|
|
|
|
|
|
|
watch([source, source2, source3], (values, oldValues) => {
|
|
|
|
expectType<(string | number)[]>(values)
|
|
|
|
expectType<(string | number)[]>(oldValues)
|
|
|
|
})
|
|
|
|
|
|
|
|
// const array
|
|
|
|
watch([source, source2, source3] as const, (values, oldValues) => {
|
|
|
|
expectType<Readonly<[string, string, number]>>(values)
|
|
|
|
expectType<Readonly<[string, string, number]>>(oldValues)
|
|
|
|
})
|
|
|
|
|
2020-02-18 12:26:25 +08:00
|
|
|
// immediate watcher's oldValue will be undefined on first run.
|
2020-02-14 01:08:21 +08:00
|
|
|
watch(
|
|
|
|
source,
|
|
|
|
(value, oldValue) => {
|
|
|
|
expectType<string>(value)
|
2020-02-18 12:14:19 +08:00
|
|
|
expectType<string | undefined>(oldValue)
|
2020-02-14 01:08:21 +08:00
|
|
|
},
|
2020-02-18 12:14:19 +08:00
|
|
|
{ immediate: true }
|
2020-02-14 01:08:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
watch(
|
|
|
|
[source, source2, source3],
|
|
|
|
(values, oldValues) => {
|
|
|
|
expectType<(string | number)[]>(values)
|
2020-02-18 12:14:19 +08:00
|
|
|
expectType<(string | number | undefined)[]>(oldValues)
|
2020-02-14 01:08:21 +08:00
|
|
|
},
|
2020-02-18 12:14:19 +08:00
|
|
|
{ immediate: true }
|
2020-02-14 01:08:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// const array
|
|
|
|
watch(
|
|
|
|
[source, source2, source3] as const,
|
|
|
|
(values, oldValues) => {
|
|
|
|
expectType<Readonly<[string, string, number]>>(values)
|
2020-02-18 12:14:19 +08:00
|
|
|
expectType<
|
|
|
|
Readonly<[string | undefined, string | undefined, number | undefined]>
|
|
|
|
>(oldValues)
|
2020-02-14 01:08:21 +08:00
|
|
|
},
|
2020-02-18 12:14:19 +08:00
|
|
|
{ immediate: true }
|
2020-02-14 01:08:21 +08:00
|
|
|
)
|
2020-02-26 08:44:06 +08:00
|
|
|
|
|
|
|
// should provide correct ref.value inner type to callbacks
|
|
|
|
const nestedRefSource = ref({
|
|
|
|
foo: ref(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(nestedRefSource, (v, ov) => {
|
|
|
|
expectType<{ foo: number }>(v)
|
|
|
|
expectType<{ foo: number }>(ov)
|
|
|
|
})
|