refTransformMacros.test-d.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { WritableComputedRef } from '@vue/reactivity'
  2. import { expectType, ref, computed, Ref, ComputedRef } from './index'
  3. import 'vue/ref-macros'
  4. import { RefType, RefTypes } from 'vue/ref-macros'
  5. // wrapping refs
  6. // normal
  7. let n = $(ref(1))
  8. n = 2
  9. // @ts-expect-error
  10. n = 'foo'
  11. // #4499 nullable
  12. let msg = $(ref<string | null>(null))
  13. msg = 'hello world'
  14. msg = null
  15. expectType<RefTypes.Ref | undefined>(msg![RefType])
  16. // computed
  17. let m = $(computed(() => n + 1))
  18. m * 1
  19. // @ts-expect-error
  20. m.slice()
  21. expectType<RefTypes.ComputedRef | undefined>(m[RefType])
  22. // writable computed
  23. let wc = $(
  24. computed({
  25. get: () => n + 1,
  26. set: v => (n = v - 1)
  27. })
  28. )
  29. wc = 2
  30. // @ts-expect-error
  31. wc = 'foo'
  32. expectType<RefTypes.WritableComputedRef | undefined>(wc[RefType])
  33. // destructure
  34. function useFoo() {
  35. let x = $ref(1)
  36. let y = $computed(() => 'hi')
  37. return $$({
  38. x,
  39. y,
  40. z: 123
  41. })
  42. }
  43. const fooRes = useFoo()
  44. const { x, y, z } = $(fooRes)
  45. expectType<number>(x)
  46. expectType<string>(y)
  47. expectType<number>(z)
  48. // $ref
  49. expectType<number>($ref(1))
  50. expectType<number>($ref(ref(1)))
  51. expectType<{ foo: number }>($ref({ foo: ref(1) }))
  52. // $shallowRef
  53. expectType<number>($shallowRef(1))
  54. expectType<{ foo: Ref<number> }>($shallowRef({ foo: ref(1) }))
  55. // $computed
  56. expectType<number>($computed(() => 1))
  57. let b = $ref(1)
  58. expectType<number>(
  59. $computed(() => b, {
  60. onTrack() {}
  61. })
  62. )
  63. // writable computed
  64. expectType<number>(
  65. $computed({
  66. get: () => 1,
  67. set: () => {}
  68. })
  69. )
  70. // $$
  71. const xRef = $$(x)
  72. expectType<Ref<number>>(xRef)
  73. const yRef = $$(y)
  74. expectType<ComputedRef<string>>(yRef)
  75. const c = $computed(() => 1)
  76. const cRef = $$(c)
  77. expectType<ComputedRef<number>>(cRef)
  78. const c2 = $computed({
  79. get: () => 1,
  80. set: () => {}
  81. })
  82. const c2Ref = $$(c2)
  83. expectType<WritableComputedRef<number>>(c2Ref)
  84. // $$ on object
  85. const obj = $$({
  86. n,
  87. m,
  88. wc
  89. })
  90. expectType<Ref<number>>(obj.n)
  91. expectType<ComputedRef<number>>(obj.m)
  92. expectType<WritableComputedRef<number>>(obj.wc)