structuredCloneMethods
Reports JSON.parse(JSON.stringify()) patterns that can use structuredClone.
✅ This rule is included in the ts logical presets.
The JSON.parse(JSON.stringify()) pattern has long been used for deep cloning objects in JavaScript.
However, the modern structuredClone() API provides a native, more robust alternative.
Benefits of structuredClone() include:
- Native API with better performance
- Proper handling circular references
- Support for more data types (Date, RegExp, Map, Set, ArrayBuffer, etc.)
- Meaningful thrown errors for non-cloneable values
This rule reports on any JSON.parse(JSON.stringify()) that can be replaced with structuredClone().
Examples
Section titled “Examples”const clone = JSON.parse(JSON.stringify(obj));function deepCopy<T>(value: T): T { return JSON.parse(JSON.stringify(value));}const clone = structuredClone(obj);function deepCopy<T>(value: T): T { return structuredClone(value);}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project relies on the different, older semantics of JSON, you might need to disable this rule.
You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-structured-clone - Oxlint:
unicorn/prefer-structured-clone
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.