2
0

ref.test-d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import {
  2. type ComputedRef,
  3. type MaybeRef,
  4. type MaybeRefOrGetter,
  5. type Ref,
  6. type ShallowRef,
  7. type ToRefs,
  8. computed,
  9. isRef,
  10. proxyRefs,
  11. reactive,
  12. readonly,
  13. ref,
  14. shallowReactive,
  15. shallowRef,
  16. toRef,
  17. toRefs,
  18. toValue,
  19. unref,
  20. } from 'vue'
  21. import { type IsAny, type IsUnion, describe, expectType } 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. // any value should return Ref<any>, not any
  72. const a = ref(1 as any)
  73. expectType<IsAny<typeof a>>(false)
  74. }
  75. plainType(1)
  76. function bailType(arg: HTMLElement | Ref<HTMLElement>) {
  77. // ref coercing
  78. const coerced = ref(arg)
  79. expectType<Ref<HTMLElement>>(coerced)
  80. // isRef as type guard
  81. if (isRef(arg)) {
  82. expectType<Ref<HTMLElement>>(arg)
  83. }
  84. // ref unwrapping
  85. expectType<HTMLElement>(unref(arg))
  86. // ref inner type should be unwrapped
  87. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  88. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  89. expectType<{ foo: HTMLElement }>(nestedRef.value)
  90. }
  91. const el = document.createElement('DIV')
  92. bailType(el)
  93. function withSymbol() {
  94. const customSymbol = Symbol()
  95. const obj = {
  96. [Symbol.asyncIterator]: ref(1),
  97. [Symbol.hasInstance]: { a: ref('a') },
  98. [Symbol.isConcatSpreadable]: { b: ref(true) },
  99. [Symbol.iterator]: [ref(1)],
  100. [Symbol.match]: new Set<Ref<number>>(),
  101. [Symbol.matchAll]: new Map<number, Ref<string>>(),
  102. [Symbol.replace]: { arr: [ref('a')] },
  103. [Symbol.search]: { set: new Set<Ref<number>>() },
  104. [Symbol.species]: { map: new Map<number, Ref<string>>() },
  105. [Symbol.split]: new WeakSet<Ref<boolean>>(),
  106. [Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
  107. [Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
  108. [Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
  109. [customSymbol]: { arr: [ref(1)] },
  110. }
  111. const objRef = ref(obj)
  112. expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
  113. expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
  114. expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
  115. expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
  116. expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
  117. expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
  118. expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
  119. expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
  120. expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
  121. expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
  122. expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
  123. expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
  124. objRef.value[Symbol.toStringTag],
  125. )
  126. expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
  127. objRef.value[Symbol.unscopables],
  128. )
  129. expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
  130. }
  131. withSymbol()
  132. const state = reactive({
  133. foo: {
  134. value: 1,
  135. label: 'bar',
  136. },
  137. })
  138. expectType<string>(state.foo.label)
  139. describe('ref with generic', <T extends { name: string }>() => {
  140. const r = {} as T
  141. const s = ref(r)
  142. expectType<string>(s.value.name)
  143. const rr = {} as MaybeRef<T>
  144. // should at least allow casting
  145. const ss = ref(rr) as Ref<T>
  146. expectType<string>(ss.value.name)
  147. })
  148. describe('allow getter and setter types to be unrelated', <T>() => {
  149. const a = { b: ref(0) }
  150. const c = ref(a)
  151. c.value = a
  152. const d = {} as T
  153. const e = ref(d)
  154. e.value = d
  155. })
  156. // shallowRef
  157. type Status = 'initial' | 'ready' | 'invalidating'
  158. const shallowStatus = shallowRef<Status>('initial')
  159. if (shallowStatus.value === 'initial') {
  160. expectType<Ref<Status>>(shallowStatus)
  161. expectType<Status>(shallowStatus.value)
  162. shallowStatus.value = 'invalidating'
  163. }
  164. const refStatus = ref<Status>('initial')
  165. if (refStatus.value === 'initial') {
  166. expectType<Ref<Status>>(shallowStatus)
  167. expectType<Status>(shallowStatus.value)
  168. refStatus.value = 'invalidating'
  169. }
  170. {
  171. const shallow = shallowRef(1)
  172. expectType<Ref<number>>(shallow)
  173. expectType<ShallowRef<number>>(shallow)
  174. }
  175. {
  176. //#7852
  177. type Steps = { step: '1' } | { step: '2' }
  178. const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
  179. const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
  180. expectType<IsUnion<typeof shallowUnionGenParam>>(false)
  181. expectType<IsUnion<typeof shallowUnionAsCast>>(false)
  182. }
  183. {
  184. // any value should return Ref<any>, not any
  185. const a = shallowRef(1 as any)
  186. expectType<IsAny<typeof a>>(false)
  187. }
  188. describe('shallowRef with generic', <T extends { name: string }>() => {
  189. const r = {} as T
  190. const s = shallowRef(r)
  191. expectType<string>(s.value.name)
  192. expectType<ShallowRef<T>>(shallowRef(r))
  193. const rr = {} as MaybeRef<T>
  194. // should at least allow casting
  195. const ss = shallowRef(rr) as Ref<T> | ShallowRef<T>
  196. expectType<string>(ss.value.name)
  197. })
  198. {
  199. // should return ShallowRef<T> | Ref<T>, not ShallowRef<T | Ref<T>>
  200. expectType<ShallowRef<{ name: string }> | Ref<{ name: string }>>(
  201. shallowRef({} as MaybeRef<{ name: string }>),
  202. )
  203. expectType<ShallowRef<number> | Ref<string[]> | ShallowRef<string>>(
  204. shallowRef('' as Ref<string[]> | string | number),
  205. )
  206. }
  207. // proxyRefs: should return `reactive` directly
  208. const r1 = reactive({
  209. k: 'v',
  210. })
  211. const p1 = proxyRefs(r1)
  212. expectType<typeof r1>(p1)
  213. // proxyRefs: `ShallowUnwrapRef`
  214. const r2 = {
  215. a: ref(1),
  216. c: computed(() => 1),
  217. u: undefined,
  218. obj: {
  219. k: ref('foo'),
  220. },
  221. union: Math.random() > 0 - 5 ? ref({ name: 'yo' }) : null,
  222. }
  223. const p2 = proxyRefs(r2)
  224. expectType<number>(p2.a)
  225. expectType<number>(p2.c)
  226. expectType<undefined>(p2.u)
  227. expectType<Ref<string>>(p2.obj.k)
  228. expectType<{ name: string } | null>(p2.union)
  229. // toRef and toRefs
  230. {
  231. const obj: {
  232. a: number
  233. b: Ref<number>
  234. c: number | string
  235. } = {
  236. a: 1,
  237. b: ref(1),
  238. c: 1,
  239. }
  240. // toRef
  241. expectType<Ref<number>>(toRef(obj, 'a'))
  242. expectType<Ref<number>>(toRef(obj, 'b'))
  243. // Should not distribute Refs over union
  244. expectType<Ref<number | string>>(toRef(obj, 'c'))
  245. expectType<Ref<number>>(toRef(() => 123))
  246. expectType<Ref<number | string>>(toRef(() => obj.c))
  247. const r = toRef(() => 123)
  248. // @ts-expect-error
  249. r.value = 234
  250. // toRefs
  251. expectType<{
  252. a: Ref<number>
  253. b: Ref<number>
  254. // Should not distribute Refs over union
  255. c: Ref<number | string>
  256. }>(toRefs(obj))
  257. // Both should not do any unwrapping
  258. const someReactive = shallowReactive({
  259. a: {
  260. b: ref(42),
  261. },
  262. })
  263. const toRefResult = toRef(someReactive, 'a')
  264. const toRefsResult = toRefs(someReactive)
  265. expectType<Ref<number>>(toRefResult.value.b)
  266. expectType<Ref<number>>(toRefsResult.a.value.b)
  267. // #5188
  268. const props = { foo: 1 } as { foo: any }
  269. const { foo } = toRefs(props)
  270. expectType<Ref<any>>(foo)
  271. }
  272. // toRef default value
  273. {
  274. const obj: { x?: number } = {}
  275. const x = toRef(obj, 'x', 1)
  276. expectType<Ref<number>>(x)
  277. }
  278. // readonly() + ref()
  279. expectType<Readonly<Ref<number>>>(readonly(ref(1)))
  280. // #2687
  281. interface AppData {
  282. state: 'state1' | 'state2' | 'state3'
  283. }
  284. const data: ToRefs<AppData> = toRefs(
  285. reactive({
  286. state: 'state1',
  287. }),
  288. )
  289. switch (data.state.value) {
  290. case 'state1':
  291. data.state.value = 'state2'
  292. break
  293. case 'state2':
  294. data.state.value = 'state3'
  295. break
  296. case 'state3':
  297. data.state.value = 'state1'
  298. break
  299. }
  300. // #3954
  301. function testUnrefGenerics<T>(p: T | Ref<T>) {
  302. expectType<T>(unref(p))
  303. }
  304. testUnrefGenerics(1)
  305. // #4771
  306. describe('shallow reactive in reactive', () => {
  307. const baz = reactive({
  308. foo: shallowReactive({
  309. a: {
  310. b: ref(42),
  311. },
  312. }),
  313. })
  314. const foo = toRef(baz, 'foo')
  315. expectType<Ref<number>>(foo.value.a.b)
  316. expectType<number>(foo.value.a.b.value)
  317. })
  318. describe('shallow ref in reactive', () => {
  319. const x = reactive({
  320. foo: shallowRef({
  321. bar: {
  322. baz: ref(123),
  323. qux: reactive({
  324. z: ref(123),
  325. }),
  326. },
  327. }),
  328. })
  329. expectType<Ref<number>>(x.foo.bar.baz)
  330. expectType<number>(x.foo.bar.qux.z)
  331. })
  332. describe('ref in shallow ref', () => {
  333. const x = shallowRef({
  334. a: ref(123),
  335. })
  336. expectType<Ref<number>>(x.value.a)
  337. })
  338. describe('reactive in shallow ref', () => {
  339. const x = shallowRef({
  340. a: reactive({
  341. b: ref(0),
  342. }),
  343. })
  344. expectType<number>(x.value.a.b)
  345. })
  346. describe('toRef <-> toValue', () => {
  347. function foo(
  348. a: MaybeRef<string>,
  349. b: () => string,
  350. c: MaybeRefOrGetter<string>,
  351. d: ComputedRef<string>,
  352. ) {
  353. const r = toRef(a)
  354. expectType<Ref<string>>(r)
  355. // writable
  356. r.value = 'foo'
  357. const rb = toRef(b)
  358. expectType<Readonly<Ref<string>>>(rb)
  359. // @ts-expect-error ref created from getter should be readonly
  360. rb.value = 'foo'
  361. const rc = toRef(c)
  362. expectType<Readonly<Ref<string> | Ref<string>>>(rc)
  363. // @ts-expect-error ref created from MaybeReadonlyRef should be readonly
  364. rc.value = 'foo'
  365. const rd = toRef(d)
  366. expectType<ComputedRef<string>>(rd)
  367. // @ts-expect-error ref created from computed ref should be readonly
  368. rd.value = 'foo'
  369. expectType<string>(toValue(a))
  370. expectType<string>(toValue(b))
  371. expectType<string>(toValue(c))
  372. expectType<string>(toValue(d))
  373. return {
  374. r: toValue(r),
  375. rb: toValue(rb),
  376. rc: toValue(rc),
  377. rd: toValue(rd),
  378. }
  379. }
  380. expectType<{
  381. r: string
  382. rb: string
  383. rc: string
  384. rd: string
  385. }>(
  386. foo(
  387. 'foo',
  388. () => 'bar',
  389. ref('baz'),
  390. computed(() => 'hi'),
  391. ),
  392. )
  393. })
  394. // unref
  395. declare const text: ShallowRef<string> | ComputedRef<string> | MaybeRef<string>
  396. expectType<string>(unref(text))