refTransformMacros.test-d.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { WritableComputedRef } from '@vue/reactivity'
  2. import { expectType, ref, Ref, ComputedRef } from './index'
  3. import 'vue/ref-macros'
  4. // wrapping refs
  5. // normal
  6. // computed
  7. // writable computed
  8. // destructure
  9. const { x, y, z } = $(useFoo())
  10. expectType<number>(x)
  11. expectType<string>(y)
  12. expectType<number>(z)
  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. // $$
  43. expectType<Ref<number>>($$(x))
  44. expectType<Ref<string>>($$(y))
  45. const c = $computed(() => 1)
  46. const cRef = $$(c)
  47. expectType<ComputedRef<number>>(cRef)
  48. const c2 = $computed({
  49. get: () => 1,
  50. set: () => {}
  51. })
  52. const c2Ref = $$(c2)
  53. expectType<WritableComputedRef<number>>(c2Ref)
  54. // $$ on object