refSugar.test-d.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { WritableComputedRef } from '@vue/reactivity'
  2. import {
  3. expectType,
  4. $ref,
  5. $shallowRef,
  6. $computed,
  7. $fromRefs,
  8. $raw,
  9. ref,
  10. Ref,
  11. ComputedRef
  12. } from './index'
  13. // $ref
  14. expectType<number>($ref(1))
  15. expectType<number>($ref(ref(1)))
  16. expectType<{ foo: number }>($ref({ foo: ref(1) }))
  17. // $shallowRef
  18. expectType<number>($shallowRef(1))
  19. expectType<{ foo: Ref<number> }>($shallowRef({ foo: ref(1) }))
  20. // $computed
  21. expectType<number>($computed(() => 1))
  22. let b = $ref(1)
  23. expectType<number>(
  24. $computed(() => b, {
  25. onTrack() {}
  26. })
  27. )
  28. // writable computed
  29. expectType<number>(
  30. $computed({
  31. get: () => 1,
  32. set: () => {}
  33. })
  34. )
  35. function useFoo() {
  36. return {
  37. x: ref(1),
  38. y: ref('hi'),
  39. z: 123
  40. }
  41. }
  42. // $fromRefs
  43. const { x, y, z } = $fromRefs(useFoo())
  44. expectType<number>(x)
  45. expectType<string>(y)
  46. expectType<number>(z)
  47. // $raw
  48. expectType<Ref<number>>($raw(x))
  49. expectType<Ref<string>>($raw(y))
  50. const c = $computed(() => 1)
  51. const cRef = $raw(c)
  52. expectType<ComputedRef<number>>(cRef)
  53. const c2 = $computed({
  54. get: () => 1,
  55. set: () => {}
  56. })
  57. const c2Ref = $raw(c2)
  58. expectType<WritableComputedRef<number>>(c2Ref)