@mmstack/primitives 19.3.6 → 19.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ /** @internal Narrows `'array'` so it is only assignable when `T` is an array type. */
2
+ type VivifyArray<T> = T extends any[] ? 'array' : never;
3
+ /** @internal Narrows `'object'` so it is only assignable when `T` is an object type. */
4
+ type VivifyObject<T> = T extends object ? 'object' : never;
5
+ /**
6
+ * Controls **autovivification** — whether, and as what shape, a writable `derived` (or `store`)
7
+ * creates a missing container when the source value is `null`/`undefined` at the moment of a
8
+ * write. Without it, writing through a nullish value is a no-op; with it, a deep write such as
9
+ * `derived(user, 'name', { vivify: 'object' }).set('Ada')` materializes the missing object
10
+ * instead of silently dropping the write.
11
+ *
12
+ * A **present** value is always preserved — updated in place for a `MutableSignal` source,
13
+ * copied for an immutable one. Vivification only ever *creates*, it never *replaces*.
14
+ *
15
+ * Variants:
16
+ * - `false` — **default.** Off; a write through a nullish source does nothing.
17
+ * - `true` / `'auto'` — infer the shape from the key: an array (`[]`) for a numeric / index key,
18
+ * a plain object (`{}`) otherwise.
19
+ * - `'object'` — always create a plain object (`{}`). Only assignable when `T` is an object.
20
+ * - `'array'` — always create an array (`[]`). Only assignable when `T` is an array.
21
+ * - `() => T` — a factory producing the container to create. Called only on a nullish source,
22
+ * once per vivification (a fresh instance each time), so a present value is never clobbered.
23
+ * Useful for seeding defaults, e.g. `() => ({ items: [], total: 0 })`.
24
+ *
25
+ * @typeParam T - The type of the container that may be created (the source/parent value).
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const user = signal<{ name: string } | null>(null);
30
+ *
31
+ * derived(user, 'name').set('Ada'); // off: dropped, user() === null
32
+ * derived(user, 'name', { vivify: 'object' }).set('Ada'); // user() === { name: 'Ada' }
33
+ * ```
34
+ */
35
+ export type Vivify<T = any> = 'auto' | boolean | (() => T) | VivifyArray<T> | VivifyObject<T>;
36
+ /**
37
+ * Options mix-in that adds an optional {@link Vivify} setting to the `options` argument of the
38
+ * `derived` / `store` key & index overloads.
39
+ *
40
+ * @typeParam T - The type of the container that may be vivified (the source/parent value).
41
+ */
42
+ export type WithVivify<T> = {
43
+ /**
44
+ * Whether, and as what shape, to create a missing container when the source value is
45
+ * `null`/`undefined` at write time. Defaults to `false` (no vivification). See {@link Vivify}.
46
+ */
47
+ vivify?: Vivify<T>;
48
+ };
49
+ /**
50
+ * @internal
51
+ * A resolved vivification function, produced by {@link createVivify}. Given the `current` value
52
+ * and the `key` about to be written, it returns the container to write into: the current value
53
+ * when present, or a freshly created one when `current` is `null`/`undefined`.
54
+ */
55
+ export type VivifyFn<T> = (current: T, key: PropertyKey) => T;
56
+ /**
57
+ * @internal
58
+ * Resolves a {@link Vivify} option into a {@link VivifyFn}. The returned function leaves a
59
+ * present value untouched and only creates a new container — object, array, or factory result —
60
+ * when the current value is `null`/`undefined`.
61
+ */
62
+ export declare function createVivify<T>(option: Vivify<T>): VivifyFn<T>;
63
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/primitives",
3
- "version": "19.3.6",
3
+ "version": "19.3.8",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "signals",
File without changes