ref-macros.d.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {
  2. Ref,
  3. UnwrapRef,
  4. ComputedRef,
  5. WritableComputedOptions,
  6. DebuggerOptions,
  7. WritableComputedRef
  8. } from '@vue/runtime-dom'
  9. declare const RefType: unique symbol
  10. declare const enum RefTypes {
  11. Ref = 1,
  12. ComputedRef = 2,
  13. WritableComputedRef = 3
  14. }
  15. type RefValue<T> = T extends null | undefined
  16. ? T
  17. : T & { [RefType]?: RefTypes.Ref }
  18. type ComputedRefValue<T> = T extends null | undefined
  19. ? T
  20. : T & { [RefType]?: RefTypes.ComputedRef }
  21. type WritableComputedRefValue<T> = T extends null | undefined
  22. ? T
  23. : T & { [RefType]?: RefTypes.WritableComputedRef }
  24. type NormalObject<T extends object> = T & { [RefType]?: never }
  25. /**
  26. * Vue ref transform macro for binding refs as reactive variables.
  27. */
  28. declare function _$<T>(arg: ComputedRef<T>): ComputedRefValue<T>
  29. declare function _$<T>(arg: WritableComputedRef<T>): WritableComputedRefValue<T>
  30. declare function _$<T>(arg: Ref<T>): RefValue<T>
  31. declare function _$<T extends object>(arg?: T): DestructureRefs<T>
  32. type DestructureRefs<T extends object> = {
  33. [K in keyof T]: T[K] extends ComputedRef<infer V>
  34. ? ComputedRefValue<V>
  35. : T[K] extends WritableComputedRef<infer V>
  36. ? WritableComputedRefValue<V>
  37. : T[K] extends Ref<infer V>
  38. ? RefValue<V>
  39. : T[K]
  40. }
  41. /**
  42. * Vue ref transform macro for accessing underlying refs of reactive varaibles.
  43. */
  44. declare function _$$<T extends object>(arg: NormalObject<T>): ToRawRefs<T>
  45. declare function _$$<T>(value: RefValue<T>): Ref<T>
  46. declare function _$$<T>(value: ComputedRefValue<T>): ComputedRef<T>
  47. declare function _$$<T>(
  48. value: WritableComputedRefValue<T>
  49. ): WritableComputedRef<T>
  50. type ToRawRefs<T extends object> = {
  51. [K in keyof T]: T[K] extends RefValue<infer V>
  52. ? Ref<V>
  53. : T[K] extends ComputedRefValue<infer V>
  54. ? ComputedRef<V>
  55. : T[K] extends WritableComputedRefValue<infer V>
  56. ? WritableComputedRef<V>
  57. : T[K] extends object
  58. ? T[K] extends
  59. | Function
  60. | Map<any, any>
  61. | Set<any>
  62. | WeakMap<any, any>
  63. | WeakSet<any>
  64. ? T[K]
  65. : ToRawRefs<T[K]>
  66. : T[K]
  67. }
  68. declare function _$ref<T>(arg?: T | Ref<T>): RefValue<UnwrapRef<T>>
  69. declare function _$shallowRef<T>(arg?: T): RefValue<T>
  70. declare function _$computed<T>(
  71. getter: () => T,
  72. debuggerOptions?: DebuggerOptions
  73. ): ComputedRefValue<T>
  74. declare function _$computed<T>(
  75. options: WritableComputedOptions<T>,
  76. debuggerOptions?: DebuggerOptions
  77. ): WritableComputedRefValue<T>
  78. declare global {
  79. const $: typeof _$
  80. const $$: typeof _$$
  81. const $ref: typeof _$ref
  82. const $shallowRef: typeof _$shallowRef
  83. const $computed: typeof _$computed
  84. }