Skip to content

Commit f9c0d99

Browse files
Refactor src/core/json-utils.ts to replace any with unknown and improve type safety. (#206)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent c9ccaa2 commit f9c0d99

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

src/core/json-utils.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
const MAX_DEPTH = 1000;
1111

12-
export function generateMergePatch(original: any, modified: any, depth = 0): any {
12+
export function generateMergePatch(original: unknown, modified: unknown, depth = 0): unknown {
1313
if (depth > MAX_DEPTH) {
1414
throw new Error('JSON merge patch depth limit exceeded');
1515
}
@@ -27,7 +27,7 @@ export function generateMergePatch(original: any, modified: any, depth = 0): any
2727
return modified;
2828
}
2929

30-
const patch: Record<string, any> = {};
30+
const patch: Record<string, unknown> = {};
3131
let hasChanges = false;
3232

3333
// Check for modifications and additions
@@ -67,7 +67,7 @@ export function generateMergePatch(original: any, modified: any, depth = 0): any
6767
* @param patch - The patch to apply
6868
* @returns The modified object (new instance or mutated)
6969
*/
70-
export function applyMergePatch(target: any, patch: any, depth = 0): any {
70+
export function applyMergePatch(target: unknown, patch: unknown, depth = 0): unknown {
7171
if (depth > MAX_DEPTH) {
7272
throw new Error('JSON apply merge patch depth limit exceeded');
7373
}
@@ -83,27 +83,29 @@ export function applyMergePatch(target: any, patch: any, depth = 0): any {
8383
return patch;
8484
}
8585

86+
let targetObj: Record<string, unknown>;
8687
if (typeof target !== 'object' || target === null || Array.isArray(target)) {
8788
// If target is not an object (or is null/array), it is treated as empty object for patching.
88-
target = {};
89+
targetObj = {};
8990
} else {
9091
// Clone target to avoid mutation if we want immutability,
9192
// Clone shallowly for safety.
92-
target = { ...target };
93+
targetObj = { ...(target as Record<string, unknown>) };
9394
}
9495

95-
for (const key of Object.keys(patch)) {
96-
const val = patch[key];
96+
const patchObj = patch as Record<string, unknown>;
97+
for (const key of Object.keys(patchObj)) {
98+
const val = patchObj[key];
9799
if (val === null) {
98-
delete target[key];
100+
delete targetObj[key];
99101
} else {
100-
target[key] = applyMergePatch(target[key], val, depth + 1);
102+
targetObj[key] = applyMergePatch(targetObj[key], val, depth + 1);
101103
}
102104
}
103105

104-
return target;
106+
return targetObj;
105107
}
106108

107-
function isObject(val: any): boolean {
109+
function isObject(val: unknown): val is Record<string, unknown> {
108110
return val !== null && typeof val === 'object';
109111
}

0 commit comments

Comments
 (0)