watch.test-d.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { ref, computed, watch } from './index'
  2. import { expectType } from 'tsd'
  3. const source = ref('foo')
  4. const source2 = computed(() => source.value)
  5. const source3 = () => 1
  6. // eager watcher's oldValue will be undefined on first run.
  7. watch(source, (value, oldValue) => {
  8. expectType<string>(value)
  9. expectType<string | undefined>(oldValue)
  10. })
  11. watch([source, source2, source3], (values, oldValues) => {
  12. expectType<(string | number)[]>(values)
  13. expectType<(string | number | undefined)[]>(oldValues)
  14. })
  15. // const array
  16. watch([source, source2, source3] as const, (values, oldValues) => {
  17. expectType<Readonly<[string, string, number]>>(values)
  18. expectType<
  19. Readonly<[string | undefined, string | undefined, number | undefined]>
  20. >(oldValues)
  21. })
  22. // lazy watcher will have consistent types for oldValue.
  23. watch(
  24. source,
  25. (value, oldValue) => {
  26. expectType<string>(value)
  27. expectType<string>(oldValue)
  28. },
  29. { lazy: true }
  30. )
  31. watch(
  32. [source, source2, source3],
  33. (values, oldValues) => {
  34. expectType<(string | number)[]>(values)
  35. expectType<(string | number)[]>(oldValues)
  36. },
  37. { lazy: true }
  38. )
  39. // const array
  40. watch(
  41. [source, source2, source3] as const,
  42. (values, oldValues) => {
  43. expectType<Readonly<[string, string, number]>>(values)
  44. expectType<Readonly<[string, string, number]>>(oldValues)
  45. },
  46. { lazy: true }
  47. )