macros.d.ts 3.1 KB

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