reactivity.d.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. declare type BaseTypes = string | number | boolean;
  2. declare type Builtin = Primitive | Function | Date | Error | RegExp;
  3. declare type CollectionTypes = IterableCollections | WeakCollections;
  4. export declare function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>;
  5. export declare function computed<T>(options: WritableComputedOptions<T>): WritableComputedRef<T>;
  6. export declare type ComputedGetter<T> = (ctx?: any) => T;
  7. export declare interface ComputedRef<T = any> extends WritableComputedRef<T> {
  8. readonly value: T;
  9. }
  10. export declare type ComputedSetter<T> = (v: T) => void;
  11. export declare function customRef<T>(factory: CustomRefFactory<T>): Ref<T>;
  12. declare type CustomRefFactory<T> = (track: () => void, trigger: () => void) => {
  13. get: () => T;
  14. set: (value: T) => void;
  15. };
  16. export declare type DebuggerEvent = {
  17. effect: ReactiveEffect;
  18. target: object;
  19. type: TrackOpTypes | TriggerOpTypes;
  20. key: any;
  21. } & DebuggerEventExtraInfo;
  22. declare interface DebuggerEventExtraInfo {
  23. newValue?: any;
  24. oldValue?: any;
  25. oldTarget?: Map<any, any> | Set<any>;
  26. }
  27. export declare type DeepReadonly<T> = T extends Builtin ? T : T extends Map<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends WeakMap<infer K, infer V> ? WeakMap<DeepReadonly<K>, DeepReadonly<V>> : T extends Set<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends WeakSet<infer U> ? WeakSet<DeepReadonly<U>> : T extends Promise<infer U> ? Promise<DeepReadonly<U>> : T extends {} ? {
  28. readonly [K in keyof T]: DeepReadonly<T[K]>;
  29. } : Readonly<T>;
  30. declare type Dep = Set<ReactiveEffect>;
  31. export declare function effect<T = any>(fn: () => T, options?: ReactiveEffectOptions): ReactiveEffect<T>;
  32. export declare function enableTracking(): void;
  33. export declare function isProxy(value: unknown): boolean;
  34. export declare function isReactive(value: unknown): boolean;
  35. export declare function isReadonly(value: unknown): boolean;
  36. export declare function isRef<T>(r: Ref<T> | unknown): r is Ref<T>;
  37. declare type IterableCollections = Map<any, any> | Set<any>;
  38. export declare const ITERATE_KEY: unique symbol;
  39. export declare function markRaw<T extends object>(value: T): T;
  40. export declare function pauseTracking(): void;
  41. declare type Primitive = string | number | boolean | bigint | symbol | undefined | null;
  42. export declare function proxyRefs<T extends object>(objectWithRefs: T): ShallowUnwrapRef<T>;
  43. /**
  44. * Creates a reactive copy of the original object.
  45. *
  46. * The reactive conversion is "deep"—it affects all nested properties. In the
  47. * ES2015 Proxy based implementation, the returned proxy is **not** equal to the
  48. * original object. It is recommended to work exclusively with the reactive
  49. * proxy and avoid relying on the original object.
  50. *
  51. * A reactive object also automatically unwraps refs contained in it, so you
  52. * don't need to use `.value` when accessing and mutating their value:
  53. *
  54. * ```js
  55. * const count = ref(0)
  56. * const obj = reactive({
  57. * count
  58. * })
  59. *
  60. * obj.count++
  61. * obj.count // -> 1
  62. * count.value // -> 1
  63. * ```
  64. */
  65. export declare function reactive<T extends object>(target: T): UnwrapNestedRefs<T>;
  66. export declare interface ReactiveEffect<T = any> {
  67. (): T;
  68. _isEffect: true;
  69. id: number;
  70. active: boolean;
  71. raw: () => T;
  72. deps: Array<Dep>;
  73. options: ReactiveEffectOptions;
  74. allowRecurse: boolean;
  75. }
  76. export declare interface ReactiveEffectOptions {
  77. lazy?: boolean;
  78. scheduler?: (job: ReactiveEffect) => void;
  79. onTrack?: (event: DebuggerEvent) => void;
  80. onTrigger?: (event: DebuggerEvent) => void;
  81. onStop?: () => void;
  82. allowRecurse?: boolean;
  83. }
  84. export declare const enum ReactiveFlags {
  85. SKIP = "__v_skip",
  86. IS_REACTIVE = "__v_isReactive",
  87. IS_READONLY = "__v_isReadonly",
  88. RAW = "__v_raw"
  89. }
  90. /**
  91. * Creates a readonly copy of the original object. Note the returned copy is not
  92. * made reactive, but `readonly` can be called on an already reactive object.
  93. */
  94. export declare function readonly<T extends object>(target: T): DeepReadonly<UnwrapNestedRefs<T>>;
  95. export declare interface Ref<T = any> {
  96. value: T;
  97. /**
  98. * Type differentiator only.
  99. * We need this to be in public d.ts but don't want it to show up in IDE
  100. * autocomplete, so we use a private Symbol instead.
  101. */
  102. [RefSymbol]: true;
  103. /* Excluded from this release type: _shallow */
  104. }
  105. export declare function ref<T extends object>(value: T): ToRef<T>;
  106. export declare function ref<T>(value: T): Ref<UnwrapRef<T>>;
  107. export declare function ref<T = any>(): Ref<T | undefined>;
  108. declare const RefSymbol: unique symbol;
  109. /**
  110. * This is a special exported interface for other packages to declare
  111. * additional types that should bail out for ref unwrapping. For example
  112. * \@vue/runtime-dom can declare it like so in its d.ts:
  113. *
  114. * ``` ts
  115. * declare module '@vue/reactivity' {
  116. * export interface RefUnwrapBailTypes {
  117. * runtimeDOMBailTypes: Node | Window
  118. * }
  119. * }
  120. * ```
  121. *
  122. * Note that api-extractor somehow refuses to include `declare module`
  123. * augmentations in its generated d.ts, so we have to manually append them
  124. * to the final generated d.ts in our build process.
  125. */
  126. export declare interface RefUnwrapBailTypes {
  127. }
  128. export declare function resetTracking(): void;
  129. /**
  130. * Return a shallowly-reactive copy of the original object, where only the root
  131. * level properties are reactive. It also does not auto-unwrap refs (even at the
  132. * root level).
  133. */
  134. export declare function shallowReactive<T extends object>(target: T): T;
  135. /**
  136. * Returns a reactive-copy of the original object, where only the root level
  137. * properties are readonly, and does NOT unwrap refs nor recursively convert
  138. * returned properties.
  139. * This is used for creating the props proxy object for stateful components.
  140. */
  141. export declare function shallowReadonly<T extends object>(target: T): Readonly<{
  142. [K in keyof T]: UnwrapNestedRefs<T[K]>;
  143. }>;
  144. export declare function shallowRef<T extends object>(value: T): T extends Ref ? T : Ref<T>;
  145. export declare function shallowRef<T>(value: T): Ref<T>;
  146. export declare function shallowRef<T = any>(): Ref<T | undefined>;
  147. export declare type ShallowUnwrapRef<T> = {
  148. [K in keyof T]: T[K] extends Ref<infer V> ? V : T[K];
  149. };
  150. declare function stop_2(effect: ReactiveEffect): void;
  151. export { stop_2 as stop }
  152. declare type SymbolExtract<T> = (T extends {
  153. [Symbol.asyncIterator]: infer V;
  154. } ? {
  155. [Symbol.asyncIterator]: V;
  156. } : {}) & (T extends {
  157. [Symbol.hasInstance]: infer V;
  158. } ? {
  159. [Symbol.hasInstance]: V;
  160. } : {}) & (T extends {
  161. [Symbol.isConcatSpreadable]: infer V;
  162. } ? {
  163. [Symbol.isConcatSpreadable]: V;
  164. } : {}) & (T extends {
  165. [Symbol.iterator]: infer V;
  166. } ? {
  167. [Symbol.iterator]: V;
  168. } : {}) & (T extends {
  169. [Symbol.match]: infer V;
  170. } ? {
  171. [Symbol.match]: V;
  172. } : {}) & (T extends {
  173. [Symbol.matchAll]: infer V;
  174. } ? {
  175. [Symbol.matchAll]: V;
  176. } : {}) & (T extends {
  177. [Symbol.replace]: infer V;
  178. } ? {
  179. [Symbol.replace]: V;
  180. } : {}) & (T extends {
  181. [Symbol.search]: infer V;
  182. } ? {
  183. [Symbol.search]: V;
  184. } : {}) & (T extends {
  185. [Symbol.species]: infer V;
  186. } ? {
  187. [Symbol.species]: V;
  188. } : {}) & (T extends {
  189. [Symbol.split]: infer V;
  190. } ? {
  191. [Symbol.split]: V;
  192. } : {}) & (T extends {
  193. [Symbol.toPrimitive]: infer V;
  194. } ? {
  195. [Symbol.toPrimitive]: V;
  196. } : {}) & (T extends {
  197. [Symbol.toStringTag]: infer V;
  198. } ? {
  199. [Symbol.toStringTag]: V;
  200. } : {}) & (T extends {
  201. [Symbol.unscopables]: infer V;
  202. } ? {
  203. [Symbol.unscopables]: V;
  204. } : {});
  205. export declare function toRaw<T>(observed: T): T;
  206. declare type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>;
  207. export declare function toRef<T extends object, K extends keyof T>(object: T, key: K): ToRef<T[K]>;
  208. export declare type ToRefs<T = any> = {
  209. [K in keyof T]: T[K] extends Ref ? T[K] : Ref<UnwrapRef<T[K]>>;
  210. };
  211. export declare function toRefs<T extends object>(object: T): ToRefs<T>;
  212. export declare function track(target: object, type: TrackOpTypes, key: unknown): void;
  213. export declare const enum TrackOpTypes {
  214. GET = "get",
  215. HAS = "has",
  216. ITERATE = "iterate"
  217. }
  218. export declare function trigger(target: object, type: TriggerOpTypes, key?: unknown, newValue?: unknown, oldValue?: unknown, oldTarget?: Map<unknown, unknown> | Set<unknown>): void;
  219. export declare const enum TriggerOpTypes {
  220. SET = "set",
  221. ADD = "add",
  222. DELETE = "delete",
  223. CLEAR = "clear"
  224. }
  225. export declare function triggerRef(ref: Ref): void;
  226. export declare function unref<T>(ref: T): T extends Ref<infer V> ? V : T;
  227. declare type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>;
  228. declare type UnwrappedObject<T> = {
  229. [P in keyof T]: UnwrapRef<T[P]>;
  230. } & SymbolExtract<T>;
  231. export declare type UnwrapRef<T> = T extends Ref<infer V> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>;
  232. declare type UnwrapRefSimple<T> = T extends Function | CollectionTypes | BaseTypes | Ref | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] ? T : T extends Array<any> ? {
  233. [K in keyof T]: UnwrapRefSimple<T[K]>;
  234. } : T extends object ? UnwrappedObject<T> : T;
  235. declare type WeakCollections = WeakMap<any, any> | WeakSet<any>;
  236. export declare interface WritableComputedOptions<T> {
  237. get: ComputedGetter<T>;
  238. set: ComputedSetter<T>;
  239. }
  240. export declare interface WritableComputedRef<T> extends Ref<T> {
  241. readonly effect: ReactiveEffect<T>;
  242. }
  243. export { }