ref.test-d.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {
  2. Ref,
  3. ref,
  4. shallowRef,
  5. isRef,
  6. unref,
  7. reactive,
  8. expectType,
  9. proxyRefs,
  10. toRef,
  11. toRefs,
  12. ToRefs
  13. } from './index'
  14. function plainType(arg: number | Ref<number>) {
  15. // ref coercing
  16. const coerced = ref(arg)
  17. expectType<Ref<number>>(coerced)
  18. // isRef as type guard
  19. if (isRef(arg)) {
  20. expectType<Ref<number>>(arg)
  21. }
  22. // ref unwrapping
  23. expectType<number>(unref(arg))
  24. // ref inner type should be unwrapped
  25. const nestedRef = ref({
  26. foo: ref(1)
  27. })
  28. expectType<{ foo: number }>(nestedRef.value)
  29. // ref boolean
  30. const falseRef = ref(false)
  31. expectType<Ref<boolean>>(falseRef)
  32. expectType<boolean>(falseRef.value)
  33. // ref true
  34. const trueRef = ref<true>(true)
  35. expectType<Ref<true>>(trueRef)
  36. expectType<true>(trueRef.value)
  37. // tuple
  38. expectType<[number, string]>(unref(ref([1, '1'])))
  39. interface IteratorFoo {
  40. [Symbol.iterator]: any
  41. }
  42. // with symbol
  43. expectType<Ref<IteratorFoo | null | undefined>>(
  44. ref<IteratorFoo | null | undefined>()
  45. )
  46. // should not unwrap ref inside arrays
  47. const arr = ref([1, new Map<string, any>(), ref('1')]).value
  48. const value = arr[0]
  49. if (isRef(value)) {
  50. expectType<Ref>(value)
  51. } else if (typeof value === 'number') {
  52. expectType<number>(value)
  53. } else {
  54. // should narrow down to Map type
  55. // and not contain any Ref type
  56. expectType<Map<string, any>>(value)
  57. }
  58. // should still unwrap in objects nested in arrays
  59. const arr2 = ref([{ a: ref(1) }]).value
  60. expectType<number>(arr2[0].a)
  61. }
  62. plainType(1)
  63. function bailType(arg: HTMLElement | Ref<HTMLElement>) {
  64. // ref coercing
  65. const coerced = ref(arg)
  66. expectType<Ref<HTMLElement>>(coerced)
  67. // isRef as type guard
  68. if (isRef(arg)) {
  69. expectType<Ref<HTMLElement>>(arg)
  70. }
  71. // ref unwrapping
  72. expectType<HTMLElement>(unref(arg))
  73. // ref inner type should be unwrapped
  74. // eslint-disable-next-line no-restricted-globals
  75. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  76. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  77. expectType<{ foo: HTMLElement }>(nestedRef.value)
  78. }
  79. // eslint-disable-next-line no-restricted-globals
  80. const el = document.createElement('DIV')
  81. bailType(el)
  82. function withSymbol() {
  83. const customSymbol = Symbol()
  84. const obj = {
  85. [Symbol.asyncIterator]: { a: 1 },
  86. [Symbol.unscopables]: { b: '1' },
  87. [customSymbol]: { c: [1, 2, 3] }
  88. }
  89. const objRef = ref(obj)
  90. expectType<{ a: number }>(objRef.value[Symbol.asyncIterator])
  91. expectType<{ b: string }>(objRef.value[Symbol.unscopables])
  92. expectType<{ c: Array<number> }>(objRef.value[customSymbol])
  93. }
  94. withSymbol()
  95. const state = reactive({
  96. foo: {
  97. value: 1,
  98. label: 'bar'
  99. }
  100. })
  101. expectType<string>(state.foo.label)
  102. // shallowRef
  103. type Status = 'initial' | 'ready' | 'invalidating'
  104. const shallowStatus = shallowRef<Status>('initial')
  105. if (shallowStatus.value === 'initial') {
  106. expectType<Ref<Status>>(shallowStatus)
  107. expectType<Status>(shallowStatus.value)
  108. shallowStatus.value = 'invalidating'
  109. }
  110. const refStatus = ref<Status>('initial')
  111. if (refStatus.value === 'initial') {
  112. expectType<Ref<Status>>(shallowStatus)
  113. expectType<Status>(shallowStatus.value)
  114. refStatus.value = 'invalidating'
  115. }
  116. // proxyRefs: should return `reactive` directly
  117. const r1 = reactive({
  118. k: 'v'
  119. })
  120. const p1 = proxyRefs(r1)
  121. expectType<typeof r1>(p1)
  122. // proxyRefs: `ShallowUnwrapRef`
  123. const r2 = {
  124. a: ref(1),
  125. obj: {
  126. k: ref('foo')
  127. }
  128. }
  129. const p2 = proxyRefs(r2)
  130. expectType<number>(p2.a)
  131. expectType<Ref<string>>(p2.obj.k)
  132. // toRef
  133. const obj = {
  134. a: 1,
  135. b: ref(1)
  136. }
  137. expectType<Ref<number>>(toRef(obj, 'a'))
  138. expectType<Ref<number>>(toRef(obj, 'b'))
  139. // toRefs
  140. const objRefs = toRefs(obj)
  141. expectType<{
  142. a: Ref<number>
  143. b: Ref<number>
  144. }>(objRefs)
  145. // #2687
  146. interface AppData {
  147. state: 'state1' | 'state2' | 'state3'
  148. }
  149. const data: ToRefs<AppData> = toRefs(
  150. reactive({
  151. state: 'state1'
  152. })
  153. )
  154. switch (data.state.value) {
  155. case 'state1':
  156. data.state.value = 'state2'
  157. break
  158. case 'state2':
  159. data.state.value = 'state3'
  160. break
  161. case 'state3':
  162. data.state.value = 'state1'
  163. break
  164. }