Skip to content

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().

const clone = JSON.parse(JSON.stringify(obj));
function deepCopy<T>(value: T): T {
return JSON.parse(JSON.stringify(value));
}

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.