ref.test-d.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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, IsAny } 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. // eslint-disable-next-line no-restricted-globals
  88. const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
  89. expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
  90. expectType<{ foo: HTMLElement }>(nestedRef.value)
  91. }
  92. // eslint-disable-next-line no-restricted-globals
  93. const el = document.createElement('DIV')
  94. bailType(el)
  95. function withSymbol() {
  96. const customSymbol = Symbol()
  97. const obj = {
  98. [Symbol.asyncIterator]: ref(1),
  99. [Symbol.hasInstance]: { a: ref('a') },
  100. [Symbol.isConcatSpreadable]: { b: ref(true) },
  101. [Symbol.iterator]: [ref(1)],
  102. [Symbol.match]: new Set<Ref<number>>(),
  103. [Symbol.matchAll]: new Map<number, Ref<string>>(),
  104. [Symbol.replace]: { arr: [ref('a')] },
  105. [Symbol.search]: { set: new Set<Ref<number>>() },
  106. [Symbol.species]: { map: new Map<number, Ref<string>>() },
  107. [Symbol.split]: new WeakSet<Ref<boolean>>(),
  108. [Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
  109. [Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
  110. [Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
  111. [customSymbol]: { arr: [ref(1)] }
  112. }
  113. const objRef = ref(obj)
  114. expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
  115. expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
  116. expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
  117. expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
  118. expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
  119. expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
  120. expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
  121. expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
  122. expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
  123. expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
  124. expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
  125. expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
  126. objRef.value[Symbol.toStringTag]
  127. )
  128. expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
  129. objRef.value[Symbol.unscopables]
  130. )
  131. expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
  132. }
  133. withSymbol()
  134. const state = reactive({
  135. foo: {
  136. value: 1,
  137. label: 'bar'
  138. }
  139. })
  140. expectType<string>(state.foo.label)
  141. // shallowRef
  142. type Status = 'initial' | 'ready' | 'invalidating'
  143. const shallowStatus = shallowRef<Status>('initial')
  144. if (shallowStatus.value === 'initial') {
  145. expectType<Ref<Status>>(shallowStatus)
  146. expectType<Status>(shallowStatus.value)
  147. shallowStatus.value = 'invalidating'
  148. }
  149. const refStatus = ref<Status>('initial')
  150. if (refStatus.value === 'initial') {
  151. expectType<Ref<Status>>(shallowStatus)
  152. expectType<Status>(shallowStatus.value)
  153. refStatus.value = 'invalidating'
  154. }
  155. {
  156. const shallow = shallowRef(1)
  157. expectType<Ref<number>>(shallow)
  158. expectType<ShallowRef<number>>(shallow)
  159. }
  160. {
  161. //#7852
  162. type Steps = { step: '1' } | { step: '2' }
  163. const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
  164. const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)
  165. expectType<IsUnion<typeof shallowUnionGenParam>>(false)
  166. expectType<IsUnion<typeof shallowUnionAsCast>>(false)
  167. }
  168. {
  169. // any value should return Ref<any>, not any
  170. const a = shallowRef(1 as any)
  171. expectType<IsAny<typeof a>>(false)
  172. }
  173. describe('shallowRef with generic', <T>() => {
  174. const r = ref({}) as MaybeRef<T>
  175. expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
  176. })
  177. // proxyRefs: should return `reactive` directly
  178. const r1 = reactive({
  179. k: 'v'
  180. })
  181. const p1 = proxyRefs(r1)
  182. expectType<typeof r1>(p1)
  183. // proxyRefs: `ShallowUnwrapRef`
  184. const r2 = {
  185. a: ref(1),
  186. obj: {
  187. k: ref('foo')
  188. }
  189. }
  190. const p2 = proxyRefs(r2)
  191. expectType<number>(p2.a)
  192. expectType<Ref<string>>(p2.obj.k)
  193. // toRef and toRefs
  194. {
  195. const obj: {
  196. a: number
  197. b: Ref<number>
  198. c: number | string
  199. } = {
  200. a: 1,
  201. b: ref(1),
  202. c: 1
  203. }
  204. // toRef
  205. expectType<Ref<number>>(toRef(obj, 'a'))
  206. expectType<Ref<number>>(toRef(obj, 'b'))
  207. // Should not distribute Refs over union
  208. expectType<Ref<number | string>>(toRef(obj, 'c'))
  209. expectType<Ref<number>>(toRef(() => 123))
  210. expectType<Ref<number | string>>(toRef(() => obj.c))
  211. const r = toRef(() => 123)
  212. // @ts-expect-error
  213. r.value = 234
  214. // toRefs
  215. expectType<{
  216. a: Ref<number>
  217. b: Ref<number>
  218. // Should not distribute Refs over union
  219. c: Ref<number | string>
  220. }>(toRefs(obj))
  221. // Both should not do any unwrapping
  222. const someReactive = shallowReactive({
  223. a: {
  224. b: ref(42)
  225. }
  226. })
  227. const toRefResult = toRef(someReactive, 'a')
  228. const toRefsResult = toRefs(someReactive)
  229. expectType<Ref<number>>(toRefResult.value.b)
  230. expectType<Ref<number>>(toRefsResult.a.value.b)
  231. // #5188
  232. const props = { foo: 1 } as { foo: any }
  233. const { foo } = toRefs(props)
  234. expectType<Ref<any>>(foo)
  235. }
  236. // toRef default value
  237. {
  238. const obj: { x?: number } = {}
  239. const x = toRef(obj, 'x', 1)
  240. expectType<Ref<number>>(x)
  241. }
  242. // readonly() + ref()
  243. expectType<Readonly<Ref<number>>>(readonly(ref(1)))
  244. // #2687
  245. interface AppData {
  246. state: 'state1' | 'state2' | 'state3'
  247. }
  248. const data: ToRefs<AppData> = toRefs(
  249. reactive({
  250. state: 'state1'
  251. })
  252. )
  253. switch (data.state.value) {
  254. case 'state1':
  255. data.state.value = 'state2'
  256. break
  257. case 'state2':
  258. data.state.value = 'state3'
  259. break
  260. case 'state3':
  261. data.state.value = 'state1'
  262. break
  263. }
  264. // #3954
  265. function testUnrefGenerics<T>(p: T | Ref<T>) {
  266. expectType<T>(unref(p))
  267. }
  268. testUnrefGenerics(1)
  269. // #4771
  270. describe('shallow reactive in reactive', () => {
  271. const baz = reactive({
  272. foo: shallowReactive({
  273. a: {
  274. b: ref(42)
  275. }
  276. })
  277. })
  278. const foo = toRef(baz, 'foo')
  279. expectType<Ref<number>>(foo.value.a.b)
  280. expectType<number>(foo.value.a.b.value)
  281. })
  282. describe('shallow ref in reactive', () => {
  283. const x = reactive({
  284. foo: shallowRef({
  285. bar: {
  286. baz: ref(123),
  287. qux: reactive({
  288. z: ref(123)
  289. })
  290. }
  291. })
  292. })
  293. expectType<Ref<number>>(x.foo.bar.baz)
  294. expectType<number>(x.foo.bar.qux.z)
  295. })
  296. describe('ref in shallow ref', () => {
  297. const x = shallowRef({
  298. a: ref(123)
  299. })
  300. expectType<Ref<number>>(x.value.a)
  301. })
  302. describe('reactive in shallow ref', () => {
  303. const x = shallowRef({
  304. a: reactive({
  305. b: ref(0)
  306. })
  307. })
  308. expectType<number>(x.value.a.b)
  309. })
  310. describe('toRef <-> toValue', () => {
  311. function foo(
  312. a: MaybeRef<string>,
  313. b: () => string,
  314. c: MaybeRefOrGetter<string>,
  315. d: ComputedRef<string>
  316. ) {
  317. const r = toRef(a)
  318. expectType<Ref<string>>(r)
  319. // writable
  320. r.value = 'foo'
  321. const rb = toRef(b)
  322. expectType<Readonly<Ref<string>>>(rb)
  323. // @ts-expect-error ref created from getter should be readonly
  324. rb.value = 'foo'
  325. const rc = toRef(c)
  326. expectType<Readonly<Ref<string> | Ref<string>>>(rc)
  327. // @ts-expect-error ref created from MaybeReadonlyRef should be readonly
  328. rc.value = 'foo'
  329. const rd = toRef(d)
  330. expectType<ComputedRef<string>>(rd)
  331. // @ts-expect-error ref created from computed ref should be readonly
  332. rd.value = 'foo'
  333. expectType<string>(toValue(a))
  334. expectType<string>(toValue(b))
  335. expectType<string>(toValue(c))
  336. expectType<string>(toValue(d))
  337. return {
  338. r: toValue(r),
  339. rb: toValue(rb),
  340. rc: toValue(rc),
  341. rd: toValue(rd)
  342. }
  343. }
  344. expectType<{
  345. r: string
  346. rb: string
  347. rc: string
  348. rd: string
  349. }>(
  350. foo(
  351. 'foo',
  352. () => 'bar',
  353. ref('baz'),
  354. computed(() => 'hi')
  355. )
  356. )
  357. })