ref.test-d.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. readonly
  15. } from './index'
  16. function plainType(arg: number | Ref<number>) {
  17. // ref coercing
  18. const coerced = ref(arg)
  19. expectType<Ref<number>>(coerced)
  20. // isRef as type guard
  21. if (isRef(arg)) {
  22. expectType<Ref<number>>(arg)
  23. }
  24. // ref unwrapping
  25. expectType<number>(unref(arg))
  26. // ref inner type should be unwrapped
  27. const nestedRef = ref({
  28. foo: ref(1)
  29. })
  30. expectType<{ foo: number }>(nestedRef.value)
  31. // ref boolean
  32. const falseRef = ref(false)
  33. expectType<Ref<boolean>>(falseRef)
  34. expectType<boolean>(falseRef.value)
  35. // ref true
  36. const trueRef = ref<true>(true)
  37. expectType<Ref<true>>(trueRef)
  38. expectType<true>(trueRef.value)
  39. // tuple
  40. expectType<[number, string]>(unref(ref([1, '1'])))
  41. interface IteratorFoo {
  42. [Symbol.iterator]: any
  43. }
  44. // with symbol
  45. expectType<Ref<IteratorFoo | null | undefined>>(
  46. ref<IteratorFoo | null | undefined>()
  47. )
  48. // should not unwrap ref inside arrays
  49. const arr = ref([1, new Map<string, any>(), ref('1')]).value
  50. const value = arr[0]
  51. if (isRef(value)) {
  52. expectType<Ref>(value)
  53. } else if (typeof value === 'number') {
  54. expectType<number>(value)
  55. } else {
  56. // should narrow down to Map type
  57. // and not contain any Ref type
  58. expectType<Map<string, any>>(value)
  59. }
  60. // should still unwrap in objects nested in arrays
  61. const arr2 = ref([{ a: ref(1) }]).value
  62. expectType<number>(arr2[0].a)
  63. }
  64. plainType(1)
  65. function bailType(arg: HTMLElement | Ref<HTMLElement>) {
  66. // ref coercing
  67. const coerced = ref(arg)
  68. expectType<Ref<HTMLElement>>(coerced)
  69. // isRef as type guard
  70. if (isRef(arg)) {
  71. expectType<Ref<HTMLElement>>(arg)
  72. }
  73. // ref unwrapping
  74. expectType<HTMLElement>(unref(arg))
  75. // ref inner type should be unwrapped
  76. // eslint-disable-next-line no-restricted-globals
  77. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  78. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  79. expectType<{ foo: HTMLElement }>(nestedRef.value)
  80. }
  81. // eslint-disable-next-line no-restricted-globals
  82. const el = document.createElement('DIV')
  83. bailType(el)
  84. function withSymbol() {
  85. const customSymbol = Symbol()
  86. const obj = {
  87. [Symbol.asyncIterator]: ref(1),
  88. [Symbol.hasInstance]: { a: ref('a') },
  89. [Symbol.isConcatSpreadable]: { b: ref(true) },
  90. [Symbol.iterator]: [ref(1)],
  91. [Symbol.match]: new Set<Ref<number>>(),
  92. [Symbol.matchAll]: new Map<number, Ref<string>>(),
  93. [Symbol.replace]: { arr: [ref('a')] },
  94. [Symbol.search]: { set: new Set<Ref<number>>() },
  95. [Symbol.species]: { map: new Map<number, Ref<string>>() },
  96. [Symbol.split]: new WeakSet<Ref<boolean>>(),
  97. [Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
  98. [Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
  99. [Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
  100. [customSymbol]: { arr: [ref(1)] }
  101. }
  102. const objRef = ref(obj)
  103. expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
  104. expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
  105. expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
  106. expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
  107. expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
  108. expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
  109. expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
  110. expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
  111. expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
  112. expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
  113. expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
  114. expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
  115. objRef.value[Symbol.toStringTag]
  116. )
  117. expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
  118. objRef.value[Symbol.unscopables]
  119. )
  120. expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
  121. }
  122. withSymbol()
  123. const state = reactive({
  124. foo: {
  125. value: 1,
  126. label: 'bar'
  127. }
  128. })
  129. expectType<string>(state.foo.label)
  130. // shallowRef
  131. type Status = 'initial' | 'ready' | 'invalidating'
  132. const shallowStatus = shallowRef<Status>('initial')
  133. if (shallowStatus.value === 'initial') {
  134. expectType<Ref<Status>>(shallowStatus)
  135. expectType<Status>(shallowStatus.value)
  136. shallowStatus.value = 'invalidating'
  137. }
  138. const refStatus = ref<Status>('initial')
  139. if (refStatus.value === 'initial') {
  140. expectType<Ref<Status>>(shallowStatus)
  141. expectType<Status>(shallowStatus.value)
  142. refStatus.value = 'invalidating'
  143. }
  144. // proxyRefs: should return `reactive` directly
  145. const r1 = reactive({
  146. k: 'v'
  147. })
  148. const p1 = proxyRefs(r1)
  149. expectType<typeof r1>(p1)
  150. // proxyRefs: `ShallowUnwrapRef`
  151. const r2 = {
  152. a: ref(1),
  153. obj: {
  154. k: ref('foo')
  155. }
  156. }
  157. const p2 = proxyRefs(r2)
  158. expectType<number>(p2.a)
  159. expectType<Ref<string>>(p2.obj.k)
  160. // toRef and toRefs
  161. {
  162. const obj: {
  163. a: number
  164. b: Ref<number>
  165. c: number | string
  166. } = {
  167. a: 1,
  168. b: ref(1),
  169. c: 1
  170. }
  171. // toRef
  172. expectType<Ref<number>>(toRef(obj, 'a'))
  173. expectType<Ref<number>>(toRef(obj, 'b'))
  174. // Should not distribute Refs over union
  175. expectType<Ref<number | string>>(toRef(obj, 'c'))
  176. // toRefs
  177. expectType<{
  178. a: Ref<number>
  179. b: Ref<number>
  180. // Should not distribute Refs over union
  181. c: Ref<number | string>
  182. }>(toRefs(obj))
  183. // Both should not do any unwrapping
  184. const someReactive = shallowReactive({
  185. a: {
  186. b: ref(42)
  187. }
  188. })
  189. const toRefResult = toRef(someReactive, 'a')
  190. const toRefsResult = toRefs(someReactive)
  191. expectType<Ref<number>>(toRefResult.value.b)
  192. expectType<Ref<number>>(toRefsResult.a.value.b)
  193. // #5188
  194. const props = { foo: 1 } as { foo: any }
  195. const { foo } = toRefs(props)
  196. expectType<Ref<any>>(foo)
  197. }
  198. // toRef default value
  199. {
  200. const obj: { x?: number } = {}
  201. const x = toRef(obj, 'x', 1)
  202. expectType<Ref<number>>(x)
  203. }
  204. // readonly() + ref()
  205. expectType<Readonly<Ref<number>>>(readonly(ref(1)))
  206. // #2687
  207. interface AppData {
  208. state: 'state1' | 'state2' | 'state3'
  209. }
  210. const data: ToRefs<AppData> = toRefs(
  211. reactive({
  212. state: 'state1'
  213. })
  214. )
  215. switch (data.state.value) {
  216. case 'state1':
  217. data.state.value = 'state2'
  218. break
  219. case 'state2':
  220. data.state.value = 'state3'
  221. break
  222. case 'state3':
  223. data.state.value = 'state1'
  224. break
  225. }
  226. // #3954
  227. function testUnrefGenerics<T>(p: T | Ref<T>) {
  228. expectType<T>(unref(p))
  229. }
  230. testUnrefGenerics(1)
  231. // #4771
  232. describe('shallow reactive in reactive', () => {
  233. const baz = reactive({
  234. foo: shallowReactive({
  235. a: {
  236. b: ref(42)
  237. }
  238. })
  239. })
  240. const foo = toRef(baz, 'foo')
  241. expectType<Ref<number>>(foo.value.a.b)
  242. expectType<number>(foo.value.a.b.value)
  243. })
  244. describe('shallow ref in reactive', () => {
  245. const x = reactive({
  246. foo: shallowRef({
  247. bar: {
  248. baz: ref(123),
  249. qux: reactive({
  250. z: ref(123)
  251. })
  252. }
  253. })
  254. })
  255. expectType<Ref<number>>(x.foo.bar.baz)
  256. expectType<number>(x.foo.bar.qux.z)
  257. })
  258. describe('ref in shallow ref', () => {
  259. const x = shallowRef({
  260. a: ref(123)
  261. })
  262. expectType<Ref<number>>(x.value.a)
  263. })
  264. describe('reactive in shallow ref', () => {
  265. const x = shallowRef({
  266. a: reactive({
  267. b: ref(0)
  268. })
  269. })
  270. expectType<number>(x.value.a.b)
  271. })