watch.test-d.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {
  2. computed,
  3. defineComponent,
  4. defineModel,
  5. ref,
  6. shallowRef,
  7. watch,
  8. } from 'vue'
  9. import { expectType } from './utils'
  10. const source = ref('foo')
  11. const source2 = computed(() => source.value)
  12. const source3 = () => 1
  13. // lazy watcher will have consistent types for oldValue.
  14. watch(source, (value, oldValue) => {
  15. expectType<string>(value)
  16. expectType<string>(oldValue)
  17. })
  18. watch([source, source2, source3], (values, oldValues) => {
  19. expectType<[string, string, number]>(values)
  20. expectType<[string, string, number]>(oldValues)
  21. })
  22. // const array
  23. watch([source, source2, source3] as const, (values, oldValues) => {
  24. expectType<Readonly<[string, string, number]>>(values)
  25. expectType<Readonly<[string, string, number]>>(oldValues)
  26. })
  27. // immediate watcher's oldValue will be undefined on first run.
  28. watch(
  29. source,
  30. (value, oldValue) => {
  31. expectType<string>(value)
  32. expectType<string | undefined>(oldValue)
  33. },
  34. { immediate: true },
  35. )
  36. watch(
  37. [source, source2, source3],
  38. (values, oldValues) => {
  39. expectType<[string, string, number]>(values)
  40. expectType<[string | undefined, string | undefined, number | undefined]>(
  41. oldValues,
  42. )
  43. },
  44. { immediate: true },
  45. )
  46. // const array
  47. watch(
  48. [source, source2, source3] as const,
  49. (values, oldValues) => {
  50. expectType<Readonly<[string, string, number]>>(values)
  51. expectType<
  52. Readonly<[string | undefined, string | undefined, number | undefined]>
  53. >(oldValues)
  54. },
  55. { immediate: true },
  56. )
  57. // should provide correct ref.value inner type to callbacks
  58. const nestedRefSource = ref({
  59. foo: ref(1),
  60. })
  61. watch(nestedRefSource, (v, ov) => {
  62. expectType<{ foo: number }>(v)
  63. expectType<{ foo: number }>(ov)
  64. })
  65. const someRef = ref({ test: 'test' })
  66. const otherRef = ref({ a: 'b' })
  67. watch([someRef, otherRef], values => {
  68. const value1 = values[0]
  69. // no type error
  70. console.log(value1.test)
  71. const value2 = values[1]
  72. // no type error
  73. console.log(value2.a)
  74. })
  75. // #6135
  76. defineComponent({
  77. data() {
  78. return { a: 1 }
  79. },
  80. created() {
  81. this.$watch(
  82. () => this.a,
  83. (v, ov) => {
  84. expectType<number>(v)
  85. expectType<number>(ov)
  86. },
  87. )
  88. },
  89. })
  90. {
  91. //#7852
  92. type Steps = { step: '1' } | { step: '2' }
  93. const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
  94. const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
  95. watch(shallowUnionGenParam, value => {
  96. expectType<Steps>(value)
  97. })
  98. watch(shallowUnionAsCast, value => {
  99. expectType<Steps>(value)
  100. })
  101. }
  102. {
  103. // defineModel
  104. const bool = defineModel({ default: false })
  105. watch(bool, value => {
  106. expectType<boolean>(value)
  107. })
  108. const bool1 = defineModel<boolean>()
  109. watch(bool1, value => {
  110. expectType<boolean | undefined>(value)
  111. })
  112. const msg = defineModel<string>({ required: true })
  113. watch(msg, value => {
  114. expectType<string>(value)
  115. })
  116. const arr = defineModel<string[]>({ required: true })
  117. watch(arr, value => {
  118. expectType<string[]>(value)
  119. })
  120. const obj = defineModel<{ foo: string }>({ required: true })
  121. watch(obj, value => {
  122. expectType<{ foo: string }>(value)
  123. })
  124. }