ref.test-d.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import {
  2. Ref,
  3. ref,
  4. shallowRef,
  5. isRef,
  6. unref,
  7. reactive,
  8. proxyRefs,
  9. toRef,
  10. toValue,
  11. toRefs,
  12. ToRefs,
  13. shallowReactive,
  14. readonly,
  15. MaybeRef,
  16. MaybeRefOrGetter,
  17. ComputedRef,
  18. computed,
  19. ShallowRef
  20. } from 'vue'
  21. import { expectType, describe, IsUnion } from './utils'
  22. function plainType(arg: number | Ref<number>) {
  23. // ref coercing
  24. const coerced = ref(arg)
  25. expectType<Ref<number>>(coerced)
  26. // isRef as type guard
  27. if (isRef(arg)) {
  28. expectType<Ref<number>>(arg)
  29. }
  30. // ref unwrapping
  31. expectType<number>(unref(arg))
  32. expectType<number>(toValue(arg))
  33. expectType<number>(toValue(() => 123))
  34. // ref inner type should be unwrapped
  35. const nestedRef = ref({
  36. foo: ref(1)
  37. })
  38. expectType<{ foo: number }>(nestedRef.value)
  39. // ref boolean
  40. const falseRef = ref(false)
  41. expectType<Ref<boolean>>(falseRef)
  42. expectType<boolean>(falseRef.value)
  43. // ref true
  44. const trueRef = ref<true>(true)
  45. expectType<Ref<true>>(trueRef)
  46. expectType<true>(trueRef.value)
  47. // tuple
  48. expectType<[number, string]>(unref(ref([1, '1'])))
  49. interface IteratorFoo {
  50. [Symbol.iterator]: any
  51. }
  52. // with symbol
  53. expectType<Ref<IteratorFoo | null | undefined>>(
  54. ref<IteratorFoo | null | undefined>()
  55. )
  56. // should not unwrap ref inside arrays
  57. const arr = ref([1, new Map<string, any>(), ref('1')]).value
  58. const value = arr[0]
  59. if (isRef(value)) {
  60. expectType<Ref>(value)
  61. } else if (typeof value === 'number') {
  62. expectType<number>(value)
  63. } else {
  64. // should narrow down to Map type
  65. // and not contain any Ref type
  66. expectType<Map<string, any>>(value)
  67. }
  68. // should still unwrap in objects nested in arrays
  69. const arr2 = ref([{ a: ref(1) }]).value
  70. expectType<number>(arr2[0].a)
  71. }
  72. plainType(1)
  73. function bailType(arg: HTMLElement | Ref<HTMLElement>) {
  74. // ref coercing
  75. const coerced = ref(arg)
  76. expectType<Ref<HTMLElement>>(coerced)
  77. // isRef as type guard
  78. if (isRef(arg)) {
  79. expectType<Ref<HTMLElement>>(arg)
  80. }
  81. // ref unwrapping
  82. expectType<HTMLElement>(unref(arg))
  83. // ref inner type should be unwrapped
  84. // eslint-disable-next-line no-restricted-globals
  85. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  86. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  87. expectType<{ foo: HTMLElement }>(nestedRef.value)
  88. }
  89. // eslint-disable-next-line no-restricted-globals
  90. const el = document.createElement('DIV')
  91. bailType(el)
  92. function withSymbol() {
  93. const customSymbol = Symbol()
  94. const obj = {
  95. [Symbol.asyncIterator]: ref(1),
  96. [Symbol.hasInstance]: { a: ref('a') },
  97. [Symbol.isConcatSpreadable]: { b: ref(true) },
  98. [Symbol.iterator]: [ref(1)],
  99. [Symbol.match]: new Set<Ref<number>>(),
  100. [Symbol.matchAll]: new Map<number, Ref<string>>(),
  101. [Symbol.replace]: { arr: [ref('a')] },
  102. [Symbol.search]: { set: new Set<Ref<number>>() },
  103. [Symbol.species]: { map: new Map<number, Ref<string>>() },
  104. [Symbol.split]: new WeakSet<Ref<boolean>>(),
  105. [Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
  106. [Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
  107. [Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
  108. [customSymbol]: { arr: [ref(1)] }
  109. }
  110. const objRef = ref(obj)
  111. expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
  112. expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
  113. expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
  114. expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
  115. expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
  116. expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
  117. expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
  118. expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
  119. expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
  120. expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
  121. expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
  122. expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
  123. objRef.value[Symbol.toStringTag]
  124. )
  125. expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
  126. objRef.value[Symbol.unscopables]
  127. )
  128. expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
  129. }
  130. withSymbol()
  131. const state = reactive({
  132. foo: {
  133. value: 1,
  134. label: 'bar'
  135. }
  136. })
  137. expectType<string>(state.foo.label)
  138. // shallowRef
  139. type Status = 'initial' | 'ready' | 'invalidating'
  140. const shallowStatus = shallowRef<Status>('initial')
  141. if (shallowStatus.value === 'initial') {
  142. expectType<Ref<Status>>(shallowStatus)
  143. expectType<Status>(shallowStatus.value)
  144. shallowStatus.value = 'invalidating'
  145. }
  146. const refStatus = ref<Status>('initial')
  147. if (refStatus.value === 'initial') {
  148. expectType<Ref<Status>>(shallowStatus)
  149. expectType<Status>(shallowStatus.value)
  150. refStatus.value = 'invalidating'
  151. }
  152. {
  153. const shallow = shallowRef(1)
  154. expectType<Ref<number>>(shallow)
  155. expectType<ShallowRef<number>>(shallow)
  156. }
  157. {
  158. //#7852
  159. type Steps = { step: '1' } | { step: '2' }
  160. const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
  161. const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
  162. expectType<IsUnion<typeof shallowUnionGenParam>>(false)
  163. expectType<IsUnion<typeof shallowUnionAsCast>>(false)
  164. }
  165. describe('shallowRef with generic', <T>() => {
  166. const r = ref({}) as MaybeRef<T>
  167. expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
  168. })
  169. // proxyRefs: should return `reactive` directly
  170. const r1 = reactive({
  171. k: 'v'
  172. })
  173. const p1 = proxyRefs(r1)
  174. expectType<typeof r1>(p1)
  175. // proxyRefs: `ShallowUnwrapRef`
  176. const r2 = {
  177. a: ref(1),
  178. obj: {
  179. k: ref('foo')
  180. }
  181. }
  182. const p2 = proxyRefs(r2)
  183. expectType<number>(p2.a)
  184. expectType<Ref<string>>(p2.obj.k)
  185. // toRef and toRefs
  186. {
  187. const obj: {
  188. a: number
  189. b: Ref<number>
  190. c: number | string
  191. } = {
  192. a: 1,
  193. b: ref(1),
  194. c: 1
  195. }
  196. // toRef
  197. expectType<Ref<number>>(toRef(obj, 'a'))
  198. expectType<Ref<number>>(toRef(obj, 'b'))
  199. // Should not distribute Refs over union
  200. expectType<Ref<number | string>>(toRef(obj, 'c'))
  201. expectType<Ref<number>>(toRef(() => 123))
  202. expectType<Ref<number | string>>(toRef(() => obj.c))
  203. const r = toRef(() => 123)
  204. // @ts-expect-error
  205. r.value = 234
  206. // toRefs
  207. expectType<{
  208. a: Ref<number>
  209. b: Ref<number>
  210. // Should not distribute Refs over union
  211. c: Ref<number | string>
  212. }>(toRefs(obj))
  213. // Both should not do any unwrapping
  214. const someReactive = shallowReactive({
  215. a: {
  216. b: ref(42)
  217. }
  218. })
  219. const toRefResult = toRef(someReactive, 'a')
  220. const toRefsResult = toRefs(someReactive)
  221. expectType<Ref<number>>(toRefResult.value.b)
  222. expectType<Ref<number>>(toRefsResult.a.value.b)
  223. // #5188
  224. const props = { foo: 1 } as { foo: any }
  225. const { foo } = toRefs(props)
  226. expectType<Ref<any>>(foo)
  227. }
  228. // toRef default value
  229. {
  230. const obj: { x?: number } = {}
  231. const x = toRef(obj, 'x', 1)
  232. expectType<Ref<number>>(x)
  233. }
  234. // readonly() + ref()
  235. expectType<Readonly<Ref<number>>>(readonly(ref(1)))
  236. // #2687
  237. interface AppData {
  238. state: 'state1' | 'state2' | 'state3'
  239. }
  240. const data: ToRefs<AppData> = toRefs(
  241. reactive({
  242. state: 'state1'
  243. })
  244. )
  245. switch (data.state.value) {
  246. case 'state1':
  247. data.state.value = 'state2'
  248. break
  249. case 'state2':
  250. data.state.value = 'state3'
  251. break
  252. case 'state3':
  253. data.state.value = 'state1'
  254. break
  255. }
  256. // #3954
  257. function testUnrefGenerics<T>(p: T | Ref<T>) {
  258. expectType<T>(unref(p))
  259. }
  260. testUnrefGenerics(1)
  261. // #4771
  262. describe('shallow reactive in reactive', () => {
  263. const baz = reactive({
  264. foo: shallowReactive({
  265. a: {
  266. b: ref(42)
  267. }
  268. })
  269. })
  270. const foo = toRef(baz, 'foo')
  271. expectType<Ref<number>>(foo.value.a.b)
  272. expectType<number>(foo.value.a.b.value)
  273. })
  274. describe('shallow ref in reactive', () => {
  275. const x = reactive({
  276. foo: shallowRef({
  277. bar: {
  278. baz: ref(123),
  279. qux: reactive({
  280. z: ref(123)
  281. })
  282. }
  283. })
  284. })
  285. expectType<Ref<number>>(x.foo.bar.baz)
  286. expectType<number>(x.foo.bar.qux.z)
  287. })
  288. describe('ref in shallow ref', () => {
  289. const x = shallowRef({
  290. a: ref(123)
  291. })
  292. expectType<Ref<number>>(x.value.a)
  293. })
  294. describe('reactive in shallow ref', () => {
  295. const x = shallowRef({
  296. a: reactive({
  297. b: ref(0)
  298. })
  299. })
  300. expectType<number>(x.value.a.b)
  301. })
  302. describe('toRef <-> toValue', () => {
  303. function foo(
  304. a: MaybeRef<string>,
  305. b: () => string,
  306. c: MaybeRefOrGetter<string>,
  307. d: ComputedRef<string>
  308. ) {
  309. const r = toRef(a)
  310. expectType<Ref<string>>(r)
  311. // writable
  312. r.value = 'foo'
  313. const rb = toRef(b)
  314. expectType<Readonly<Ref<string>>>(rb)
  315. // @ts-expect-error ref created from getter should be readonly
  316. rb.value = 'foo'
  317. const rc = toRef(c)
  318. expectType<Readonly<Ref<string> | Ref<string>>>(rc)
  319. // @ts-expect-error ref created from MaybeReadonlyRef should be readonly
  320. rc.value = 'foo'
  321. const rd = toRef(d)
  322. expectType<ComputedRef<string>>(rd)
  323. // @ts-expect-error ref created from computed ref should be readonly
  324. rd.value = 'foo'
  325. expectType<string>(toValue(a))
  326. expectType<string>(toValue(b))
  327. expectType<string>(toValue(c))
  328. expectType<string>(toValue(d))
  329. return {
  330. r: toValue(r),
  331. rb: toValue(rb),
  332. rc: toValue(rc),
  333. rd: toValue(rd)
  334. }
  335. }
  336. expectType<{
  337. r: string
  338. rb: string
  339. rc: string
  340. rd: string
  341. }>(
  342. foo(
  343. 'foo',
  344. () => 'bar',
  345. ref('baz'),
  346. computed(() => 'hi')
  347. )
  348. )
  349. })