ref.test-d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. watch
  14. } from './index'
  15. function plainType(arg: number | Ref<number>) {
  16. // ref coercing
  17. const coerced = ref(arg)
  18. expectType<Ref<number>>(coerced)
  19. // isRef as type guard
  20. if (isRef(arg)) {
  21. expectType<Ref<number>>(arg)
  22. }
  23. // ref unwrapping
  24. expectType<number>(unref(arg))
  25. // ref inner type should be unwrapped
  26. const nestedRef = ref({
  27. foo: ref(1)
  28. })
  29. expectType<{ foo: number }>(nestedRef.value)
  30. // ref boolean
  31. const falseRef = ref(false)
  32. expectType<Ref<boolean>>(falseRef)
  33. expectType<boolean>(falseRef.value)
  34. // ref true
  35. const trueRef = ref<true>(true)
  36. expectType<Ref<true>>(trueRef)
  37. expectType<true>(trueRef.value)
  38. // tuple
  39. expectType<[number, string]>(unref(ref([1, '1'])))
  40. interface IteratorFoo {
  41. [Symbol.iterator]: any
  42. }
  43. // with symbol
  44. expectType<Ref<IteratorFoo | null | undefined>>(
  45. ref<IteratorFoo | null | undefined>()
  46. )
  47. // should not unwrap ref inside arrays
  48. const arr = ref([1, new Map<string, any>(), ref('1')]).value
  49. const value = arr[0]
  50. if (isRef(value)) {
  51. expectType<Ref>(value)
  52. } else if (typeof value === 'number') {
  53. expectType<number>(value)
  54. } else {
  55. // should narrow down to Map type
  56. // and not contain any Ref type
  57. expectType<Map<string, any>>(value)
  58. }
  59. // should still unwrap in objects nested in arrays
  60. const arr2 = ref([{ a: ref(1) }]).value
  61. expectType<number>(arr2[0].a)
  62. }
  63. plainType(1)
  64. function bailType(arg: HTMLElement | Ref<HTMLElement>) {
  65. // ref coercing
  66. const coerced = ref(arg)
  67. expectType<Ref<HTMLElement>>(coerced)
  68. // isRef as type guard
  69. if (isRef(arg)) {
  70. expectType<Ref<HTMLElement>>(arg)
  71. }
  72. // ref unwrapping
  73. expectType<HTMLElement>(unref(arg))
  74. // ref inner type should be unwrapped
  75. // eslint-disable-next-line no-restricted-globals
  76. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  77. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  78. expectType<{ foo: HTMLElement }>(nestedRef.value)
  79. }
  80. // eslint-disable-next-line no-restricted-globals
  81. const el = document.createElement('DIV')
  82. bailType(el)
  83. function withSymbol() {
  84. const customSymbol = Symbol()
  85. const obj = {
  86. [Symbol.asyncIterator]: ref(1),
  87. [Symbol.hasInstance]: { a: ref('a') },
  88. [Symbol.isConcatSpreadable]: { b: ref(true) },
  89. [Symbol.iterator]: [ref(1)],
  90. [Symbol.match]: new Set<Ref<number>>(),
  91. [Symbol.matchAll]: new Map<number, Ref<string>>(),
  92. [Symbol.replace]: { arr: [ref('a')] },
  93. [Symbol.search]: { set: new Set<Ref<number>>() },
  94. [Symbol.species]: { map: new Map<number, Ref<string>>() },
  95. [Symbol.split]: new WeakSet<Ref<boolean>>(),
  96. [Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
  97. [Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
  98. [Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
  99. [customSymbol]: { arr: [ref(1)] }
  100. }
  101. const objRef = ref(obj)
  102. expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
  103. expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
  104. expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
  105. expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
  106. expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
  107. expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
  108. expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
  109. expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
  110. expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
  111. expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
  112. expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
  113. expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
  114. objRef.value[Symbol.toStringTag]
  115. )
  116. expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
  117. objRef.value[Symbol.unscopables]
  118. )
  119. expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
  120. }
  121. withSymbol()
  122. const state = reactive({
  123. foo: {
  124. value: 1,
  125. label: 'bar'
  126. }
  127. })
  128. expectType<string>(state.foo.label)
  129. // shallowRef
  130. type Status = 'initial' | 'ready' | 'invalidating'
  131. const shallowStatus = shallowRef<Status>('initial')
  132. if (shallowStatus.value === 'initial') {
  133. expectType<Ref<Status>>(shallowStatus)
  134. expectType<Status>(shallowStatus.value)
  135. shallowStatus.value = 'invalidating'
  136. }
  137. const refStatus = ref<Status>('initial')
  138. if (refStatus.value === 'initial') {
  139. expectType<Ref<Status>>(shallowStatus)
  140. expectType<Status>(shallowStatus.value)
  141. refStatus.value = 'invalidating'
  142. }
  143. // proxyRefs: should return `reactive` directly
  144. const r1 = reactive({
  145. k: 'v'
  146. })
  147. const p1 = proxyRefs(r1)
  148. expectType<typeof r1>(p1)
  149. // proxyRefs: `ShallowUnwrapRef`
  150. const r2 = {
  151. a: ref(1),
  152. obj: {
  153. k: ref('foo')
  154. }
  155. }
  156. const p2 = proxyRefs(r2)
  157. expectType<number>(p2.a)
  158. expectType<Ref<string>>(p2.obj.k)
  159. // toRef
  160. const obj = {
  161. a: 1,
  162. b: ref(1)
  163. }
  164. expectType<Ref<number>>(toRef(obj, 'a'))
  165. expectType<Ref<number>>(toRef(obj, 'b'))
  166. const objWithUnionProp: { a: string | number } = {
  167. a: 1
  168. }
  169. watch(toRef(objWithUnionProp, 'a'), value => {
  170. expectType<string | number>(value)
  171. })
  172. // toRefs
  173. const objRefs = toRefs(obj)
  174. expectType<{
  175. a: Ref<number>
  176. b: Ref<number>
  177. }>(objRefs)
  178. // #2687
  179. interface AppData {
  180. state: 'state1' | 'state2' | 'state3'
  181. }
  182. const data: ToRefs<AppData> = toRefs(
  183. reactive({
  184. state: 'state1'
  185. })
  186. )
  187. switch (data.state.value) {
  188. case 'state1':
  189. data.state.value = 'state2'
  190. break
  191. case 'state2':
  192. data.state.value = 'state3'
  193. break
  194. case 'state3':
  195. data.state.value = 'state1'
  196. break
  197. }
  198. // #3954
  199. function testUnrefGenerics<T>(p: T | Ref<T>) {
  200. expectType<T>(unref(p))
  201. }
  202. testUnrefGenerics(1)