JavaScript Object.assign Throws After a Partial Update
Contents
If Object.assign(liveSettings, patch) throws on a read-only property, earlier writes can still be visible through liveSettings and any aliases to it. Catching the error leaves those writes in place. For plain settings that can be replaced by reference, build a separate candidate, validate it, and publish it with a single reference assignment.
Check which writes survived
ECMAScript’s Object.assign algorithm reads each enumerable own source property and writes it to the target before processing the next key. A failed target write stops the algorithm. In the example below, the source contains three ordinary string keys in order. The first write succeeds, the second throws, and the third is never attempted.
Save this as settings.mjs and run node settings.mjs. The assertions cover the failed in-place update, a rejected replacement, and a successful replacement. The example was verified on Node.js 24.13.0 on macOS arm64.
import assert from 'node:assert/strict';
function makeSettings() { const settings = { retries: 1, region: 'local', timeoutMs: 500 }; Object.defineProperty(settings, 'region', { writable: false }); return settings;}
const badPatch = { retries: 3, region: 'remote', timeoutMs: 900 };const target = makeSettings();const alias = target;
assert.throws(() => Object.assign(target, badPatch), TypeError);assert.deepEqual(alias, { retries: 3, region: 'local', timeoutMs: 500 });
let liveSettings = makeSettings();const previous = liveSettings;
function applyPatch(patch) { const candidate = { ...liveSettings, ...patch }; if (candidate.region !== liveSettings.region) { throw new RangeError('Region cannot change'); } Object.defineProperty(candidate, 'region', { writable: false }); liveSettings = candidate;}
assert.throws(() => applyPatch(badPatch), RangeError);assert.equal(liveSettings, previous);assert.deepEqual(previous, { retries: 1, region: 'local', timeoutMs: 500 });
applyPatch({ retries: 3, timeoutMs: 900 });assert.notEqual(liveSettings, previous);assert.deepEqual(liveSettings, { retries: 3, region: 'local', timeoutMs: 900 });assert.equal(previous.retries, 1);assert.equal(Object.getOwnPropertyDescriptor(liveSettings, 'region').writable, false);console.log('Partial write, rejected patch, and successful replacement verified.');Choose where readers get their settings
Object spread creates data properties on the new object. The candidate can therefore contain the new region even though the live object’s region is read-only. Validate the region as an application rule and reapply the descriptor before exposing the candidate if future writes should still fail.
This approach works when consumers read the current reference for each operation. Code holding the previous object keeps its old values after a successful replacement. If consumers require the same object identity, the replacement pattern cannot update those aliases. The code that owns the shared object needs its own update contract.
The example uses flat primitive values and plain data properties. Nested values can still share references after a spread, and reading an enumerable getter can run code while constructing the candidate. For nested settings, rebuild the levels you intend to change before validation.