Skip to content

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 { update } from '@helpers4/object';
// or, from the all-in-one package (same code, one install):
import { update } from 'helpers4/object';
update<T extends object, P extends string | readonly PropertyKey[], V extends unknown>(obj: T, path: P, updater: function): conditional
ParameterTypeDescription
objTThe object to mutate
pathPDot/bracket-notation string literal or explicit `PropertyKey[]`
updaterfunctionCalled with the current value (`undefined` if the path is absent); its return value is written back at the path

conditional — The mutated object (same reference, narrowed type)

Equivalent to set(obj, path, updater(get(obj, path))), without repeating the path.

const state = { count: 1 };
update(state, 'count', (n) => (n ?? 0) + 1)
// => { count: 2 }

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.

const stats: Record<string, unknown> = {};
update(stats, 'hits.total', (n: number | undefined) => (n ?? 0) + 1)
// => { hits: { total: 1 } }

View source on GitHub