import type { ReadonlySignal, Signal } from "@preact-signals/unified-signals"; import { ReactiveFlags } from "./constants"; import type { UnwrapSignalSimple } from "./deepSignal"; export declare const deepReactiveMap: WeakMap; export declare const shallowReactiveMap: WeakMap; export declare const deepReadonlyMap: WeakMap; export declare const shallowReadonlyMap: WeakMap; export type UnwrapNestedSignals = T extends Signal ? T : T extends ReadonlySignal ? T : UnwrapSignalSimple; export interface Target { [ReactiveFlags.RAW]?: any; [ReactiveFlags.IS_REACTIVE]?: boolean; [ReactiveFlags.IS_READONLY]?: boolean; [ReactiveFlags.IS_SHALLOW]?: boolean; [ReactiveFlags.SKIP]?: boolean; } /** * Returns the raw, original object of a Vue-created proxy. * * `toRaw()` can return the original object from proxies created by * {@link deepReactive()}, {@link deepReadonly()}, {@link shallowReactive()} or * {@link shallowReadonly()}. * * This is an escape hatch that can be used to temporarily read without * incurring proxy access / tracking overhead or write without triggering * changes. It is **not** recommended to hold a persistent reference to the * original object. Use with caution. * * @example * ```js * const foo = {} * const reactiveFoo = reactive(foo) * * console.log(toRaw(reactiveFoo) === foo) // true * ``` * * @param observed - The object for which the "raw" value is requested. */ export declare function toRaw(observed: T): T; /** * * Returns a reactive proxy of the object. * * The reactive conversion is "deep": it affects all nested properties. A * reactive object also deeply unwraps any properties that are refs while * maintaining reactivity. * * @example * ```js * const obj = reactive({ count: 0 }) * ``` * * @param target - The object to be made reactive. * @returns */ export declare const deepReactive: (target: T) => UnwrapNestedSignals; export declare const ShallowReactiveMarker: unique symbol; export type ShallowReactive = T & { [ShallowReactiveMarker]?: true; }; /** * Shallow version of {@link deepReactive()}. * * Unlike {@link deepReactive()}, there is no deep conversion: only root-level * properties are reactive for a shallow reactive object. Property values are * stored and exposed as-is - this also means properties with ref values will * not be automatically unwrapped. * * @example * ```js * const state = shallowReactive({ * foo: 1, * nested: { * bar: 2 * } * }) * * // mutating state's own properties is reactive * state.foo++ * * // ...but does not convert nested objects * isReactive(state.nested) // false * * // NOT reactive * state.nested.bar++ * ``` * * @param target - The source object. */ export declare function shallowReactive(target: T): ShallowReactive; type Primitive = string | number | boolean | bigint | symbol | undefined | null; type Builtin = Primitive | Function | Date | Error | RegExp; export type DeepReadonly = T extends Builtin ? T : T extends Map ? ReadonlyMap, DeepReadonly> : T extends ReadonlyMap ? ReadonlyMap, DeepReadonly> : T extends WeakMap ? WeakMap, DeepReadonly> : T extends Set ? ReadonlySet> : T extends ReadonlySet ? ReadonlySet> : T extends WeakSet ? WeakSet> : T extends Promise ? Promise> : T extends Signal ? Readonly>> : T extends {} ? { readonly [K in keyof T]: DeepReadonly; } : Readonly; /** * takes an object (reactive or plain) or a ref and returns a readonly proxy to * the original. * * a readonly proxy is deep: any nested property accessed will be readonly as * well. it also has the same ref-unwrapping behavior as {@link deepreactive()}, * except the unwrapped values will also be made readonly. * * @example * ```js * const original = reactive({ count: 0 }) * * const copy = readonly(original) * * watcheffect(() => { * // works for reactivity tracking * console.log(copy.count) * }) * * // mutating original will trigger watchers relying on the copy * original.count++ * * // mutating the copy will fail and result in a warning * copy.count++ // warning! * ``` * * @param target - The source object. */ export declare function deepReadonly(target: T): DeepReadonly>; /** * Shallow version of {@link deepReadonly()}. * * Unlike {@link deepReadonly()}, there is no deep conversion: only root-level * properties are made readonly. Property values are stored and exposed as-is - * this also means properties with ref values will not be automatically * unwrapped. * * @example * ```js * const state = shallowReadonly({ * foo: 1, * nested: { * bar: 2 * } * }) * * // mutating state's own properties will fail * state.foo++ * * // ...but works on nested objects * isReadonly(state.nested) // false * * // works * state.nested.bar++ * ``` * * @param target - The source object. */ export declare function shallowReadonly(target: T): Readonly; /** * Checks if an object is a proxy created by {@link deepReactive()} or * {@link shallowReactive()} (or {@link ref()} in some cases). * * @example * ```js * isReactive(reactive({})) // => true * isReactive(readonly(reactive({}))) // => true * isReactive(ref({}).value) // => true * isReactive(readonly(ref({})).value) // => true * isReactive(ref(true)) // => false * isReactive(shallowRef({}).value) // => false * isReactive(shallowReactive({})) // => true * ``` * * @param value - The value to check. */ export declare function isReactive(value: unknown): boolean; /** * Checks whether the passed value is a readonly object. The properties of a * readonly object can change, but they can't be assigned directly via the * passed object. * * The proxies created by {@link deepReadonly()} and {@link shallowReadonly()} are * both considered readonly, as is a computed ref without a set function. * * @param value - The value to check. */ export declare function isReadonly(value: unknown): boolean; export declare function isShallow(value: unknown): boolean; /** * Checks if an object is a proxy created by {@link deepReactive}, * {@link deepReadonly}, {@link shallowReactive} or {@link shallowReadonly()}. * * @param value - The value to check. */ export declare function isProxy(value: unknown): boolean; export declare const RawSymbol: unique symbol; export type Raw = T & { [RawSymbol]?: true; }; /** * Marks an object so that it will never be converted to a proxy. Returns the * object itself. * * @example * ```js * const foo = markRaw({}) * console.log(isReactive(reactive(foo))) // false * * // also works when nested inside other reactive objects * const bar = reactive({ foo }) * console.log(isReactive(bar.foo)) // false * ``` * * **Warning:** `markRaw()` together with the shallow APIs such as * {@link shallowReactive()} allow you to selectively opt-out of the default * deep reactive/readonly conversion and embed raw, non-proxied objects in your * state graph. * * @param value - The object to be marked as "raw". */ export declare function markRaw(value: T): Raw; /** * Returns a reactive proxy of the given value (if possible). * * If the given value is not an object, the original value itself is returned. * * @param value - The value for which a reactive proxy shall be created. */ export declare const toDeepReactive: (value: T) => T; /** * Returns a readonly proxy of the given value (if possible). * * If the given value is not an object, the original value itself is returned. * * @param value - The value for which a readonly proxy shall be created. */ export declare const toDeepReadonly: (value: T) => T extends object ? DeepReadonly> : T; export {};