reactivity.d.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. import { IfAny } from '@vue/shared';
  2. export declare enum TrackOpTypes {
  3. GET = "get",
  4. HAS = "has",
  5. ITERATE = "iterate"
  6. }
  7. export declare enum TriggerOpTypes {
  8. SET = "set",
  9. ADD = "add",
  10. DELETE = "delete",
  11. CLEAR = "clear"
  12. }
  13. export declare enum ReactiveFlags {
  14. SKIP = "__v_skip",
  15. IS_REACTIVE = "__v_isReactive",
  16. IS_READONLY = "__v_isReadonly",
  17. IS_SHALLOW = "__v_isShallow",
  18. RAW = "__v_raw",
  19. IS_REF = "__v_isRef"
  20. }
  21. export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>;
  22. declare const ReactiveMarkerSymbol: unique symbol;
  23. export interface ReactiveMarker {
  24. [ReactiveMarkerSymbol]?: void;
  25. }
  26. export type Reactive<T> = UnwrapNestedRefs<T> & (T extends readonly any[] ? ReactiveMarker : {});
  27. /**
  28. * Returns a reactive proxy of the object.
  29. *
  30. * The reactive conversion is "deep": it affects all nested properties. A
  31. * reactive object also deeply unwraps any properties that are refs while
  32. * maintaining reactivity.
  33. *
  34. * @example
  35. * ```js
  36. * const obj = reactive({ count: 0 })
  37. * ```
  38. *
  39. * @param target - The source object.
  40. * @see {@link https://vuejs.org/api/reactivity-core.html#reactive}
  41. */
  42. export declare function reactive<T extends object>(target: T): Reactive<T>;
  43. declare const ShallowReactiveMarker: unique symbol;
  44. export type ShallowReactive<T> = T & {
  45. [ShallowReactiveMarker]?: true;
  46. };
  47. /**
  48. * Shallow version of {@link reactive}.
  49. *
  50. * Unlike {@link reactive}, there is no deep conversion: only root-level
  51. * properties are reactive for a shallow reactive object. Property values are
  52. * stored and exposed as-is - this also means properties with ref values will
  53. * not be automatically unwrapped.
  54. *
  55. * @example
  56. * ```js
  57. * const state = shallowReactive({
  58. * foo: 1,
  59. * nested: {
  60. * bar: 2
  61. * }
  62. * })
  63. *
  64. * // mutating state's own properties is reactive
  65. * state.foo++
  66. *
  67. * // ...but does not convert nested objects
  68. * isReactive(state.nested) // false
  69. *
  70. * // NOT reactive
  71. * state.nested.bar++
  72. * ```
  73. *
  74. * @param target - The source object.
  75. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreactive}
  76. */
  77. export declare function shallowReactive<T extends object>(target: T): ShallowReactive<T>;
  78. type Primitive = string | number | boolean | bigint | symbol | undefined | null;
  79. type Builtin = Primitive | Function | Date | Error | RegExp;
  80. export 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 Ref<infer U, unknown> ? Readonly<Ref<DeepReadonly<U>>> : T extends {} ? {
  81. readonly [K in keyof T]: DeepReadonly<T[K]>;
  82. } : Readonly<T>;
  83. /**
  84. * Takes an object (reactive or plain) or a ref and returns a readonly proxy to
  85. * the original.
  86. *
  87. * A readonly proxy is deep: any nested property accessed will be readonly as
  88. * well. It also has the same ref-unwrapping behavior as {@link reactive},
  89. * except the unwrapped values will also be made readonly.
  90. *
  91. * @example
  92. * ```js
  93. * const original = reactive({ count: 0 })
  94. *
  95. * const copy = readonly(original)
  96. *
  97. * watchEffect(() => {
  98. * // works for reactivity tracking
  99. * console.log(copy.count)
  100. * })
  101. *
  102. * // mutating original will trigger watchers relying on the copy
  103. * original.count++
  104. *
  105. * // mutating the copy will fail and result in a warning
  106. * copy.count++ // warning!
  107. * ```
  108. *
  109. * @param target - The source object.
  110. * @see {@link https://vuejs.org/api/reactivity-core.html#readonly}
  111. */
  112. export declare function readonly<T extends object>(target: T): DeepReadonly<UnwrapNestedRefs<T>>;
  113. /**
  114. * Shallow version of {@link readonly}.
  115. *
  116. * Unlike {@link readonly}, there is no deep conversion: only root-level
  117. * properties are made readonly. Property values are stored and exposed as-is -
  118. * this also means properties with ref values will not be automatically
  119. * unwrapped.
  120. *
  121. * @example
  122. * ```js
  123. * const state = shallowReadonly({
  124. * foo: 1,
  125. * nested: {
  126. * bar: 2
  127. * }
  128. * })
  129. *
  130. * // mutating state's own properties will fail
  131. * state.foo++
  132. *
  133. * // ...but works on nested objects
  134. * isReadonly(state.nested) // false
  135. *
  136. * // works
  137. * state.nested.bar++
  138. * ```
  139. *
  140. * @param target - The source object.
  141. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowreadonly}
  142. */
  143. export declare function shallowReadonly<T extends object>(target: T): Readonly<T>;
  144. /**
  145. * Checks if an object is a proxy created by {@link reactive} or
  146. * {@link shallowReactive} (or {@link ref} in some cases).
  147. *
  148. * @example
  149. * ```js
  150. * isReactive(reactive({})) // => true
  151. * isReactive(readonly(reactive({}))) // => true
  152. * isReactive(ref({}).value) // => true
  153. * isReactive(readonly(ref({})).value) // => true
  154. * isReactive(ref(true)) // => false
  155. * isReactive(shallowRef({}).value) // => false
  156. * isReactive(shallowReactive({})) // => true
  157. * ```
  158. *
  159. * @param value - The value to check.
  160. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isreactive}
  161. */
  162. export declare function isReactive(value: unknown): boolean;
  163. /**
  164. * Checks whether the passed value is a readonly object. The properties of a
  165. * readonly object can change, but they can't be assigned directly via the
  166. * passed object.
  167. *
  168. * The proxies created by {@link readonly} and {@link shallowReadonly} are
  169. * both considered readonly, as is a computed ref without a set function.
  170. *
  171. * @param value - The value to check.
  172. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isreadonly}
  173. */
  174. export declare function isReadonly(value: unknown): boolean;
  175. export declare function isShallow(value: unknown): boolean;
  176. /**
  177. * Checks if an object is a proxy created by {@link reactive},
  178. * {@link readonly}, {@link shallowReactive} or {@link shallowReadonly}.
  179. *
  180. * @param value - The value to check.
  181. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isproxy}
  182. */
  183. export declare function isProxy(value: any): boolean;
  184. /**
  185. * Returns the raw, original object of a Vue-created proxy.
  186. *
  187. * `toRaw()` can return the original object from proxies created by
  188. * {@link reactive}, {@link readonly}, {@link shallowReactive} or
  189. * {@link shallowReadonly}.
  190. *
  191. * This is an escape hatch that can be used to temporarily read without
  192. * incurring proxy access / tracking overhead or write without triggering
  193. * changes. It is **not** recommended to hold a persistent reference to the
  194. * original object. Use with caution.
  195. *
  196. * @example
  197. * ```js
  198. * const foo = {}
  199. * const reactiveFoo = reactive(foo)
  200. *
  201. * console.log(toRaw(reactiveFoo) === foo) // true
  202. * ```
  203. *
  204. * @param observed - The object for which the "raw" value is requested.
  205. * @see {@link https://vuejs.org/api/reactivity-advanced.html#toraw}
  206. */
  207. export declare function toRaw<T>(observed: T): T;
  208. export type Raw<T> = T & {
  209. [RawSymbol]?: true;
  210. };
  211. /**
  212. * Marks an object so that it will never be converted to a proxy. Returns the
  213. * object itself.
  214. *
  215. * @example
  216. * ```js
  217. * const foo = markRaw({})
  218. * console.log(isReactive(reactive(foo))) // false
  219. *
  220. * // also works when nested inside other reactive objects
  221. * const bar = reactive({ foo })
  222. * console.log(isReactive(bar.foo)) // false
  223. * ```
  224. *
  225. * **Warning:** `markRaw()` together with the shallow APIs such as
  226. * {@link shallowReactive} allow you to selectively opt-out of the default
  227. * deep reactive/readonly conversion and embed raw, non-proxied objects in your
  228. * state graph.
  229. *
  230. * @param value - The object to be marked as "raw".
  231. * @see {@link https://vuejs.org/api/reactivity-advanced.html#markraw}
  232. */
  233. export declare function markRaw<T extends object>(value: T): Raw<T>;
  234. /**
  235. * Returns a reactive proxy of the given value (if possible).
  236. *
  237. * If the given value is not an object, the original value itself is returned.
  238. *
  239. * @param value - The value for which a reactive proxy shall be created.
  240. */
  241. export declare const toReactive: <T extends unknown>(value: T) => T;
  242. /**
  243. * Returns a readonly proxy of the given value (if possible).
  244. *
  245. * If the given value is not an object, the original value itself is returned.
  246. *
  247. * @param value - The value for which a readonly proxy shall be created.
  248. */
  249. export declare const toReadonly: <T extends unknown>(value: T) => DeepReadonly<T>;
  250. export type EffectScheduler = (...args: any[]) => any;
  251. export type DebuggerEvent = {
  252. effect: Subscriber;
  253. } & DebuggerEventExtraInfo;
  254. export type DebuggerEventExtraInfo = {
  255. target: object;
  256. type: TrackOpTypes | TriggerOpTypes;
  257. key: any;
  258. newValue?: any;
  259. oldValue?: any;
  260. oldTarget?: Map<any, any> | Set<any>;
  261. };
  262. export interface DebuggerOptions {
  263. onTrack?: (event: DebuggerEvent) => void;
  264. onTrigger?: (event: DebuggerEvent) => void;
  265. }
  266. export interface ReactiveEffectOptions extends DebuggerOptions {
  267. scheduler?: EffectScheduler;
  268. allowRecurse?: boolean;
  269. onStop?: () => void;
  270. }
  271. export declare enum EffectFlags {
  272. /**
  273. * ReactiveEffect only
  274. */
  275. ACTIVE = 1,
  276. RUNNING = 2,
  277. TRACKING = 4,
  278. NOTIFIED = 8,
  279. DIRTY = 16,
  280. ALLOW_RECURSE = 32,
  281. PAUSED = 64,
  282. EVALUATED = 128
  283. }
  284. /**
  285. * Subscriber is a type that tracks (or subscribes to) a list of deps.
  286. */
  287. interface Subscriber extends DebuggerOptions {
  288. }
  289. export declare class ReactiveEffect<T = any> implements Subscriber, ReactiveEffectOptions {
  290. fn: () => T;
  291. scheduler?: EffectScheduler;
  292. onStop?: () => void;
  293. onTrack?: (event: DebuggerEvent) => void;
  294. onTrigger?: (event: DebuggerEvent) => void;
  295. constructor(fn: () => T);
  296. pause(): void;
  297. resume(): void;
  298. run(): T;
  299. stop(): void;
  300. trigger(): void;
  301. get dirty(): boolean;
  302. }
  303. export interface ReactiveEffectRunner<T = any> {
  304. (): T;
  305. effect: ReactiveEffect;
  306. }
  307. export interface ReactiveEffectRunner<T = any> {
  308. (): T;
  309. effect: ReactiveEffect;
  310. }
  311. export declare function effect<T = any>(fn: () => T, options?: ReactiveEffectOptions): ReactiveEffectRunner<T>;
  312. /**
  313. * Stops the effect associated with the given runner.
  314. *
  315. * @param runner - Association with the effect to stop tracking.
  316. */
  317. export declare function stop(runner: ReactiveEffectRunner): void;
  318. /**
  319. * Temporarily pauses tracking.
  320. */
  321. export declare function pauseTracking(): void;
  322. /**
  323. * Re-enables effect tracking (if it was paused).
  324. */
  325. export declare function enableTracking(): void;
  326. /**
  327. * Resets the previous global effect tracking state.
  328. */
  329. export declare function resetTracking(): void;
  330. /**
  331. * Registers a cleanup function for the current active effect.
  332. * The cleanup function is called right before the next effect run, or when the
  333. * effect is stopped.
  334. *
  335. * Throws a warning if there is no current active effect. The warning can be
  336. * suppressed by passing `true` to the second argument.
  337. *
  338. * @param fn - the cleanup function to be registered
  339. * @param failSilently - if `true`, will not throw warning when called without
  340. * an active effect.
  341. */
  342. export declare function onEffectCleanup(fn: () => void, failSilently?: boolean): void;
  343. declare const ComputedRefSymbol: unique symbol;
  344. declare const WritableComputedRefSymbol: unique symbol;
  345. interface BaseComputedRef<T, S = T> extends Ref<T, S> {
  346. [ComputedRefSymbol]: true;
  347. /**
  348. * @deprecated computed no longer uses effect
  349. */
  350. effect: ComputedRefImpl;
  351. }
  352. export interface ComputedRef<T = any> extends BaseComputedRef<T> {
  353. readonly value: T;
  354. }
  355. export interface WritableComputedRef<T, S = T> extends BaseComputedRef<T, S> {
  356. [WritableComputedRefSymbol]: true;
  357. }
  358. export type ComputedGetter<T> = (oldValue?: T) => T;
  359. export type ComputedSetter<T> = (newValue: T) => void;
  360. export interface WritableComputedOptions<T, S = T> {
  361. get: ComputedGetter<T>;
  362. set: ComputedSetter<S>;
  363. }
  364. /**
  365. * @private exported by @vue/reactivity for Vue core use, but not exported from
  366. * the main vue package
  367. */
  368. export declare class ComputedRefImpl<T = any> implements Subscriber {
  369. fn: ComputedGetter<T>;
  370. private readonly setter;
  371. effect: this;
  372. onTrack?: (event: DebuggerEvent) => void;
  373. onTrigger?: (event: DebuggerEvent) => void;
  374. constructor(fn: ComputedGetter<T>, setter: ComputedSetter<T> | undefined, isSSR: boolean);
  375. get value(): T;
  376. set value(newValue: T);
  377. }
  378. /**
  379. * Takes a getter function and returns a readonly reactive ref object for the
  380. * returned value from the getter. It can also take an object with get and set
  381. * functions to create a writable ref object.
  382. *
  383. * @example
  384. * ```js
  385. * // Creating a readonly computed ref:
  386. * const count = ref(1)
  387. * const plusOne = computed(() => count.value + 1)
  388. *
  389. * console.log(plusOne.value) // 2
  390. * plusOne.value++ // error
  391. * ```
  392. *
  393. * ```js
  394. * // Creating a writable computed ref:
  395. * const count = ref(1)
  396. * const plusOne = computed({
  397. * get: () => count.value + 1,
  398. * set: (val) => {
  399. * count.value = val - 1
  400. * }
  401. * })
  402. *
  403. * plusOne.value = 1
  404. * console.log(count.value) // 0
  405. * ```
  406. *
  407. * @param getter - Function that produces the next value.
  408. * @param debugOptions - For debugging. See {@link https://vuejs.org/guide/extras/reactivity-in-depth.html#computed-debugging}.
  409. * @see {@link https://vuejs.org/api/reactivity-core.html#computed}
  410. */
  411. export declare function computed<T>(getter: ComputedGetter<T>, debugOptions?: DebuggerOptions): ComputedRef<T>;
  412. export declare function computed<T, S = T>(options: WritableComputedOptions<T, S>, debugOptions?: DebuggerOptions): WritableComputedRef<T, S>;
  413. declare const RefSymbol: unique symbol;
  414. declare const RawSymbol: unique symbol;
  415. export interface Ref<T = any, S = T> {
  416. get value(): T;
  417. set value(_: S);
  418. /**
  419. * Type differentiator only.
  420. * We need this to be in public d.ts but don't want it to show up in IDE
  421. * autocomplete, so we use a private Symbol instead.
  422. */
  423. [RefSymbol]: true;
  424. }
  425. /**
  426. * Checks if a value is a ref object.
  427. *
  428. * @param r - The value to inspect.
  429. * @see {@link https://vuejs.org/api/reactivity-utilities.html#isref}
  430. */
  431. export declare function isRef<T>(r: Ref<T> | unknown): r is Ref<T>;
  432. /**
  433. * Takes an inner value and returns a reactive and mutable ref object, which
  434. * has a single property `.value` that points to the inner value.
  435. *
  436. * @param value - The object to wrap in the ref.
  437. * @see {@link https://vuejs.org/api/reactivity-core.html#ref}
  438. */
  439. export declare function ref<T>(value: T): [T] extends [Ref] ? IfAny<T, Ref<T>, T> : Ref<UnwrapRef<T>, UnwrapRef<T> | T>;
  440. export declare function ref<T = any>(): Ref<T | undefined>;
  441. declare const ShallowRefMarker: unique symbol;
  442. export type ShallowRef<T = any, S = T> = Ref<T, S> & {
  443. [ShallowRefMarker]?: true;
  444. };
  445. /**
  446. * Shallow version of {@link ref}.
  447. *
  448. * @example
  449. * ```js
  450. * const state = shallowRef({ count: 1 })
  451. *
  452. * // does NOT trigger change
  453. * state.value.count = 2
  454. *
  455. * // does trigger change
  456. * state.value = { count: 2 }
  457. * ```
  458. *
  459. * @param value - The "inner value" for the shallow ref.
  460. * @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref}
  461. */
  462. export declare function shallowRef<T>(value: T): Ref extends T ? T extends Ref ? IfAny<T, ShallowRef<T>, T> : ShallowRef<T> : ShallowRef<T>;
  463. export declare function shallowRef<T = any>(): ShallowRef<T | undefined>;
  464. /**
  465. * Force trigger effects that depends on a shallow ref. This is typically used
  466. * after making deep mutations to the inner value of a shallow ref.
  467. *
  468. * @example
  469. * ```js
  470. * const shallow = shallowRef({
  471. * greet: 'Hello, world'
  472. * })
  473. *
  474. * // Logs "Hello, world" once for the first run-through
  475. * watchEffect(() => {
  476. * console.log(shallow.value.greet)
  477. * })
  478. *
  479. * // This won't trigger the effect because the ref is shallow
  480. * shallow.value.greet = 'Hello, universe'
  481. *
  482. * // Logs "Hello, universe"
  483. * triggerRef(shallow)
  484. * ```
  485. *
  486. * @param ref - The ref whose tied effects shall be executed.
  487. * @see {@link https://vuejs.org/api/reactivity-advanced.html#triggerref}
  488. */
  489. export declare function triggerRef(ref: Ref): void;
  490. export type MaybeRef<T = any> = T | Ref<T> | ShallowRef<T> | WritableComputedRef<T>;
  491. export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => T);
  492. /**
  493. * Returns the inner value if the argument is a ref, otherwise return the
  494. * argument itself. This is a sugar function for
  495. * `val = isRef(val) ? val.value : val`.
  496. *
  497. * @example
  498. * ```js
  499. * function useFoo(x: number | Ref<number>) {
  500. * const unwrapped = unref(x)
  501. * // unwrapped is guaranteed to be number now
  502. * }
  503. * ```
  504. *
  505. * @param ref - Ref or plain value to be converted into the plain value.
  506. * @see {@link https://vuejs.org/api/reactivity-utilities.html#unref}
  507. */
  508. export declare function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T;
  509. /**
  510. * Normalizes values / refs / getters to values.
  511. * This is similar to {@link unref}, except that it also normalizes getters.
  512. * If the argument is a getter, it will be invoked and its return value will
  513. * be returned.
  514. *
  515. * @example
  516. * ```js
  517. * toValue(1) // 1
  518. * toValue(ref(1)) // 1
  519. * toValue(() => 1) // 1
  520. * ```
  521. *
  522. * @param source - A getter, an existing ref, or a non-function value.
  523. * @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue}
  524. */
  525. export declare function toValue<T>(source: MaybeRefOrGetter<T>): T;
  526. /**
  527. * Returns a proxy for the given object that shallowly unwraps properties that
  528. * are refs. If the object already is reactive, it's returned as-is. If not, a
  529. * new reactive proxy is created.
  530. *
  531. * @param objectWithRefs - Either an already-reactive object or a simple object
  532. * that contains refs.
  533. */
  534. export declare function proxyRefs<T extends object>(objectWithRefs: T): ShallowUnwrapRef<T>;
  535. export type CustomRefFactory<T> = (track: () => void, trigger: () => void) => {
  536. get: () => T;
  537. set: (value: T) => void;
  538. };
  539. /**
  540. * Creates a customized ref with explicit control over its dependency tracking
  541. * and updates triggering.
  542. *
  543. * @param factory - The function that receives the `track` and `trigger` callbacks.
  544. * @see {@link https://vuejs.org/api/reactivity-advanced.html#customref}
  545. */
  546. export declare function customRef<T>(factory: CustomRefFactory<T>): Ref<T>;
  547. export type ToRefs<T = any> = {
  548. [K in keyof T]: ToRef<T[K]>;
  549. };
  550. /**
  551. * Converts a reactive object to a plain object where each property of the
  552. * resulting object is a ref pointing to the corresponding property of the
  553. * original object. Each individual ref is created using {@link toRef}.
  554. *
  555. * @param object - Reactive object to be made into an object of linked refs.
  556. * @see {@link https://vuejs.org/api/reactivity-utilities.html#torefs}
  557. */
  558. export declare function toRefs<T extends object>(object: T): ToRefs<T>;
  559. export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>;
  560. /**
  561. * Used to normalize values / refs / getters into refs.
  562. *
  563. * @example
  564. * ```js
  565. * // returns existing refs as-is
  566. * toRef(existingRef)
  567. *
  568. * // creates a ref that calls the getter on .value access
  569. * toRef(() => props.foo)
  570. *
  571. * // creates normal refs from non-function values
  572. * // equivalent to ref(1)
  573. * toRef(1)
  574. * ```
  575. *
  576. * Can also be used to create a ref for a property on a source reactive object.
  577. * The created ref is synced with its source property: mutating the source
  578. * property will update the ref, and vice-versa.
  579. *
  580. * @example
  581. * ```js
  582. * const state = reactive({
  583. * foo: 1,
  584. * bar: 2
  585. * })
  586. *
  587. * const fooRef = toRef(state, 'foo')
  588. *
  589. * // mutating the ref updates the original
  590. * fooRef.value++
  591. * console.log(state.foo) // 2
  592. *
  593. * // mutating the original also updates the ref
  594. * state.foo++
  595. * console.log(fooRef.value) // 3
  596. * ```
  597. *
  598. * @param source - A getter, an existing ref, a non-function value, or a
  599. * reactive object to create a property ref from.
  600. * @param [key] - (optional) Name of the property in the reactive object.
  601. * @see {@link https://vuejs.org/api/reactivity-utilities.html#toref}
  602. */
  603. export declare function toRef<T>(value: T): T extends () => infer R ? Readonly<Ref<R>> : T extends Ref ? T : Ref<UnwrapRef<T>>;
  604. export declare function toRef<T extends object, K extends keyof T>(object: T, key: K): ToRef<T[K]>;
  605. export declare function toRef<T extends object, K extends keyof T>(object: T, key: K, defaultValue: T[K]): ToRef<Exclude<T[K], undefined>>;
  606. /**
  607. * This is a special exported interface for other packages to declare
  608. * additional types that should bail out for ref unwrapping. For example
  609. * \@vue/runtime-dom can declare it like so in its d.ts:
  610. *
  611. * ``` ts
  612. * declare module '@vue/reactivity' {
  613. * export interface RefUnwrapBailTypes {
  614. * runtimeDOMBailTypes: Node | Window
  615. * }
  616. * }
  617. * ```
  618. */
  619. export interface RefUnwrapBailTypes {
  620. }
  621. export type ShallowUnwrapRef<T> = {
  622. [K in keyof T]: DistributeRef<T[K]>;
  623. };
  624. type DistributeRef<T> = T extends Ref<infer V, unknown> ? V : T;
  625. export type UnwrapRef<T> = T extends ShallowRef<infer V, unknown> ? V : T extends Ref<infer V, unknown> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>;
  626. type UnwrapRefSimple<T> = T extends Builtin | Ref | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] | {
  627. [RawSymbol]?: true;
  628. } ? T : T extends Map<infer K, infer V> ? Map<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Map<any, any>>> : T extends WeakMap<infer K, infer V> ? WeakMap<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakMap<any, any>>> : T extends Set<infer V> ? Set<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Set<any>>> : T extends WeakSet<infer V> ? WeakSet<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakSet<any>>> : T extends ReadonlyArray<any> ? {
  629. [K in keyof T]: UnwrapRefSimple<T[K]>;
  630. } : T extends object & {
  631. [ShallowReactiveMarker]?: never;
  632. } ? {
  633. [P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>;
  634. } : T;
  635. export declare const ITERATE_KEY: unique symbol;
  636. export declare const MAP_KEY_ITERATE_KEY: unique symbol;
  637. export declare const ARRAY_ITERATE_KEY: unique symbol;
  638. /**
  639. * Tracks access to a reactive property.
  640. *
  641. * This will check which effect is running at the moment and record it as dep
  642. * which records all effects that depend on the reactive property.
  643. *
  644. * @param target - Object holding the reactive property.
  645. * @param type - Defines the type of access to the reactive property.
  646. * @param key - Identifier of the reactive property to track.
  647. */
  648. export declare function track(target: object, type: TrackOpTypes, key: unknown): void;
  649. /**
  650. * Finds all deps associated with the target (or a specific property) and
  651. * triggers the effects stored within.
  652. *
  653. * @param target - The reactive object.
  654. * @param type - Defines the type of the operation that needs to trigger effects.
  655. * @param key - Can be used to target a specific reactive property in the target object.
  656. */
  657. export declare function trigger(target: object, type: TriggerOpTypes, key?: unknown, newValue?: unknown, oldValue?: unknown, oldTarget?: Map<unknown, unknown> | Set<unknown>): void;
  658. export declare class EffectScope {
  659. detached: boolean;
  660. private _isPaused;
  661. constructor(detached?: boolean);
  662. get active(): boolean;
  663. pause(): void;
  664. /**
  665. * Resumes the effect scope, including all child scopes and effects.
  666. */
  667. resume(): void;
  668. run<T>(fn: () => T): T | undefined;
  669. prevScope: EffectScope | undefined;
  670. stop(fromParent?: boolean): void;
  671. }
  672. /**
  673. * Creates an effect scope object which can capture the reactive effects (i.e.
  674. * computed and watchers) created within it so that these effects can be
  675. * disposed together. For detailed use cases of this API, please consult its
  676. * corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
  677. *
  678. * @param detached - Can be used to create a "detached" effect scope.
  679. * @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
  680. */
  681. export declare function effectScope(detached?: boolean): EffectScope;
  682. /**
  683. * Returns the current active effect scope if there is one.
  684. *
  685. * @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
  686. */
  687. export declare function getCurrentScope(): EffectScope | undefined;
  688. /**
  689. * Registers a dispose callback on the current active effect scope. The
  690. * callback will be invoked when the associated effect scope is stopped.
  691. *
  692. * @param fn - The callback function to attach to the scope's cleanup.
  693. * @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
  694. */
  695. export declare function onScopeDispose(fn: () => void, failSilently?: boolean): void;
  696. /**
  697. * Track array iteration and return:
  698. * - if input is reactive: a cloned raw array with reactive values
  699. * - if input is non-reactive or shallowReactive: the original raw array
  700. */
  701. export declare function reactiveReadArray<T>(array: T[]): T[];
  702. /**
  703. * Track array iteration and return raw array
  704. */
  705. export declare function shallowReadArray<T>(arr: T[]): T[];
  706. export declare enum WatchErrorCodes {
  707. WATCH_GETTER = 2,
  708. WATCH_CALLBACK = 3,
  709. WATCH_CLEANUP = 4
  710. }
  711. export type WatchEffect = (onCleanup: OnCleanup) => void;
  712. export type WatchSource<T = any> = Ref<T, any> | ComputedRef<T> | (() => T);
  713. export type WatchCallback<V = any, OV = any> = (value: V, oldValue: OV, onCleanup: OnCleanup) => any;
  714. export type OnCleanup = (cleanupFn: () => void) => void;
  715. export interface WatchOptions<Immediate = boolean> extends DebuggerOptions {
  716. immediate?: Immediate;
  717. deep?: boolean | number;
  718. once?: boolean;
  719. scheduler?: WatchScheduler;
  720. onWarn?: (msg: string, ...args: any[]) => void;
  721. }
  722. export type WatchStopHandle = () => void;
  723. export interface WatchHandle extends WatchStopHandle {
  724. pause: () => void;
  725. resume: () => void;
  726. stop: () => void;
  727. }
  728. export type WatchScheduler = (job: () => void, isFirstRun: boolean) => void;
  729. /**
  730. * Returns the current active effect if there is one.
  731. */
  732. export declare function getCurrentWatcher(): ReactiveEffect<any> | undefined;
  733. /**
  734. * Registers a cleanup callback on the current active effect. This
  735. * registered cleanup callback will be invoked right before the
  736. * associated effect re-runs.
  737. *
  738. * @param cleanupFn - The callback function to attach to the effect's cleanup.
  739. * @param failSilently - if `true`, will not throw warning when called without
  740. * an active effect.
  741. * @param owner - The effect that this cleanup function should be attached to.
  742. * By default, the current active effect.
  743. */
  744. export declare function onWatcherCleanup(cleanupFn: () => void, failSilently?: boolean, owner?: ReactiveEffect | undefined): void;
  745. export declare function watch(source: WatchSource | WatchSource[] | WatchEffect | object, cb?: WatchCallback | null, options?: WatchOptions): WatchHandle;
  746. export declare function traverse(value: unknown, depth?: number, seen?: Set<unknown>): unknown;