ref.test-d.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. shallowReactive
  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 and toRefs
  160. {
  161. const obj: {
  162. a: number
  163. b: Ref<number>
  164. c: number | string
  165. } = {
  166. a: 1,
  167. b: ref(1),
  168. c: 1
  169. }
  170. // toRef
  171. expectType<Ref<number>>(toRef(obj, 'a'))
  172. expectType<Ref<number>>(toRef(obj, 'b'))
  173. // Should not distribute Refs over union
  174. expectType<Ref<number | string>>(toRef(obj, 'c'))
  175. // toRefs
  176. expectType<{
  177. a: Ref<number>
  178. b: Ref<number>
  179. // Should not distribute Refs over union
  180. c: Ref<number | string>
  181. }>(toRefs(obj))
  182. // Both should not do any unwrapping
  183. const someReactive = shallowReactive({
  184. a: {
  185. b: ref(42)
  186. }
  187. })
  188. const toRefResult = toRef(someReactive, 'a')
  189. const toRefsResult = toRefs(someReactive)
  190. expectType<Ref<number>>(toRefResult.value.b)
  191. expectType<Ref<number>>(toRefsResult.a.value.b)
  192. }
  193. // toRef default value
  194. {
  195. const obj: { x?: number } = {}
  196. const x = toRef(obj, 'x', 1)
  197. expectType<Ref<number>>(x)
  198. }
  199. // #2687
  200. interface AppData {
  201. state: 'state1' | 'state2' | 'state3'
  202. }
  203. const data: ToRefs<AppData> = toRefs(
  204. reactive({
  205. state: 'state1'
  206. })
  207. )
  208. switch (data.state.value) {
  209. case 'state1':
  210. data.state.value = 'state2'
  211. break
  212. case 'state2':
  213. data.state.value = 'state3'
  214. break
  215. case 'state3':
  216. data.state.value = 'state1'
  217. break
  218. }
  219. // #3954
  220. function testUnrefGenerics<T>(p: T | Ref<T>) {
  221. expectType<T>(unref(p))
  222. }
  223. testUnrefGenerics(1)
  224. // #4771
  225. describe('shallow reactive in reactive', () => {
  226. const baz = reactive({
  227. foo: shallowReactive({
  228. a: {
  229. b: ref(42)
  230. }
  231. })
  232. })
  233. const foo = toRef(baz, 'foo')
  234. expectType<Ref<number>>(foo.value.a.b)
  235. expectType<number>(foo.value.a.b.value)
  236. })
  237. describe('shallow ref in reactive', () => {
  238. const x = reactive({
  239. foo: shallowRef({
  240. bar: {
  241. baz: ref(123),
  242. qux: reactive({
  243. z: ref(123)
  244. })
  245. }
  246. })
  247. })
  248. expectType<Ref<number>>(x.foo.bar.baz)
  249. expectType<number>(x.foo.bar.qux.z)
  250. })
  251. describe('ref in shallow ref', () => {
  252. const x = shallowRef({
  253. a: ref(123)
  254. })
  255. expectType<Ref<number>>(x.value.a)
  256. })
  257. describe('reactive in shallow ref', () => {
  258. const x = shallowRef({
  259. a: reactive({
  260. b: ref(0)
  261. })
  262. })
  263. expectType<number>(x.value.a.b)
  264. })