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
const clone: any
clone
=
var JSON: JSON

An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

JSON
.
JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): any

Converts a JavaScript Object Notation (JSON) string into an object.

@paramtext A valid JSON string.

@paramreviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.

@throws{SyntaxError} If text is not valid JSON.

parse
(
var JSON: JSON

An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

JSON
.
JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

@paramvalue A JavaScript value, usually an object or array, to be converted.

@paramreplacer A function that transforms the results.

@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

@throws{TypeError} If a circular reference or a BigInt value is found.

stringify
(
const obj: any
obj
));
function
function deepCopy<T>(value: T): T
deepCopy
<
function (type parameter) T in deepCopy<T>(value: T): T
T
>(
value: T
value
:
function (type parameter) T in deepCopy<T>(value: T): T
T
):
function (type parameter) T in deepCopy<T>(value: T): T
T
{
return
var JSON: JSON

An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

JSON
.
JSON.parse(text: string, reviver?: (this: any, key: string, value: any) => any): any

Converts a JavaScript Object Notation (JSON) string into an object.

@paramtext A valid JSON string.

@paramreviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.

@throws{SyntaxError} If text is not valid JSON.

parse
(
var JSON: JSON

An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

JSON
.
JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

@paramvalue A JavaScript value, usually an object or array, to be converted.

@paramreplacer A function that transforms the results.

@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

@throws{TypeError} If a circular reference or a BigInt value is found.

stringify
(
value: T
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 ❤️‍🔥 around the world by the Flint team and contributors.