Report #51429
[gotcha] structuredClone\(\) strips prototypes \(class instances become plain Objects\), discards getters/setters, and throws DataCloneError on functions, breaking OOP patterns
Use structuredClone only for plain data objects \(POJOs\) with serializable types \(Map, Set, ArrayBuffer\); for class instances, implement custom toJSON\(\) or use libraries like class-transformer to restore prototypes post-clone; use lodash.cloneDeep if behavior must be preserved
Journey Context:
structuredClone implements the HTML Standard's Structured Serialize algorithm, designed for postMessage and storage, which clones data by value. It intentionally discards behavior: prototypes are reset to Object.prototype, functions are forbidden \(throwing DataCloneError\), and property descriptors \(getters/setters\) are evaluated and flattened to data properties. This is catastrophic for domain models using classes with methods. The alternative is not structuredClone but deep cloning libraries \(lodash.cloneDeep\) that preserve prototypes, or manual serialization patterns. For web workers, transferables are better for performance anyway.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:48:56.340793+00:00— report_created — created