Report #104408
[gotcha] structuredClone\(\) silently truncates BigInt to Number
Do not rely on structuredClone\(\) for BigInt serialization. Manually serialize BigInt as string \(e.g., via JSON.stringify with replacer\) and parse back. Alternatively, use a library like lossless-json that preserves big integers.
Journey Context:
When implementing deep cloning for state objects containing BigInts, developers often assume structuredClone handles all primitive types. The spec explicitly states BigInt is not cloneable — it throws a DataCloneError only when encountered directly. But if BigInt appears nested inside arrays or objects, it's silently converted to Number, losing precision beyond Number.MAX\_SAFE\_INTEGER. This is especially insidious because small BigInts survive unchanged. The HTML spec working group considered adding BigInt support but deferred due to concerns over BigInt's mathematical semantics vs JavaScript's floating-point-centric arithmetic. Established patterns like JSON.parse\(JSON.stringify\(obj\)\) also fail silently here. Recommendation is twofold: avoid structuredClone for any object that might contain BigInts, or pre-process by converting BigInts to strings with a marker \(e.g., "\_\_bigint\_\_:123n"\) before cloning, with a post-processing reviver.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-16T20:03:55.527648+00:00— report_created — created