All files / hooks/use-object-state/src types.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                                                                                               
/**
 * Accepted initial value for {@link useObjectState}.
 *
 * Mirrors `useState`'s lazy-initializer support: pass a plain object, or a
 * function returning one (evaluated **once** on mount). The resolved value is
 * captured for {@link ObjectStateReset | reset()}.
 *
 * @template T - The object shape held in state.
 *
 * @example
 * ```ts
 * useObjectState({ name: "", age: 0 });         // direct object
 * useObjectState(() => ({ name: "", age: 0 })); // lazy (run once)
 * ```
 */
export type ObjectStateInitializer<T extends object> = T | (() => T);
 
/**
 * The `patch` updater returned by {@link useObjectState}.
 *
 * Shallow-merges a partial into the current state **immutably** — it always
 * produces a brand-new object (`{ ...prev, ...partial }`) and never mutates the
 * previous state. Only the provided keys change; untouched keys are preserved by
 * reference.
 *
 * Two forms are accepted:
 * - a `Partial<T>` object — merged directly, and
 * - a functional updater `(prev: T) => Partial<T>` — computes the partial from
 *   the current state (use this when the next value depends on the previous one).
 *
 * The merge is **shallow**: a nested object in the partial replaces the previous
 * nested object wholesale, it is not deep-merged.
 *
 * @template T - The object shape held in state.
 */
export type ObjectStatePatch<T extends object> = (
  patch: Partial<T> | ((prev: T) => Partial<T>)
) => void;
 
/**
 * The `reset` action returned by {@link useObjectState}.
 *
 * - `reset()` — restores the state to the initial value captured on mount (if a
 *   lazy initializer was used, the value it produced is cached once and reused;
 *   the initializer is not re-run).
 * - `reset(nextState)` — replaces the state with the provided object instead.
 *
 * @template T - The object shape held in state.
 */
export type ObjectStateReset<T extends object> = (nextState?: T) => void;
 
/**
 * Return type of {@link useObjectState}: a tuple of the current state, the
 * `patch` updater, and the `reset` action — mirroring the `useState` tuple shape
 * but with partial-merge semantics.
 *
 * @template T - The object shape held in state.
 */
export type UseObjectStateReturn<T extends object> = [
  T,
  ObjectStatePatch<T>,
  ObjectStateReset<T>,
];