update
Updates the value at a path by applying a function to its current value,
creating intermediate objects as needed. Equivalent to
set(obj, path, updater(get(obj, path))) in a single call.
Uses the same path syntax and type-inference rules as get and
set — see those for the full behavior (string vs. PropertyKey[]
paths, prototype-pollution guarding, etc.).
Available since v3.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
obj | T | The object to mutate |
path | P | Dot/bracket-notation string literal or explicit `PropertyKey[]` |
updater | function | Called with the current value (`undefined` if the path is absent); its return value is written back at the path |
Returns
Section titled “Returns”conditional — The mutated object (same reference, narrowed type)
Examples
Section titled “Examples”Increment a counter in one call
Section titled “Increment a counter in one call”Equivalent to set(obj, path, updater(get(obj, path))), without repeating the path.
Missing paths create intermediate objects, like set()
Section titled “Missing paths create intermediate objects, like set()”The updater receives undefined when the path does not exist yet.
