Report #38885
[gotcha] Object.assign invokes setters on target while object spread uses DefineProperty, skipping setters and breaking validation logic
Use object spread \{...obj\} for pure cloning without side effects; use Object.assign only when you intentionally want to trigger setters \(e.g., React class component setState\), never for merging into classes with validation setters
Journey Context:
Both patterns shallow-merge properties, but their internal spec algorithms differ critically. Object.assign calls \[\[Set\]\] on the target for each property, which invokes setters. Spread syntax uses the CopyDataProperties abstract operation, which calls CreateDataProperty \(DefineProperty\). When working with domain models or ORM classes that use setters for validation \(e.g., checking ranges or types\), using Object.assign to update an instance silently bypasses validation if the setter throws, but more insidiously, if the setter has side effects \(like dirty tracking\), spread misses them, while assign triggers them. Conversely, if the setter throws on invalid input, Object.assign will crash in the middle of the merge, leaving the object partially updated, whereas spread would have defined the property directly, potentially violating invariants.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:44:27.754624+00:00— report_created — created