From 068d43226f24201776d9e78144554e8601b6bd4b Mon Sep 17 00:00:00 2001 From: yushengchen Date: Wed, 9 Apr 2025 09:24:42 +0800 Subject: [PATCH] refactor: remove `as any` of file `jsonUtils.test.ts` refactor: extract `DataType` as the return type of function `getDataType` --- client/src/utils/__tests__/jsonUtils.test.ts | 14 ++++++-------- client/src/utils/jsonUtils.ts | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/client/src/utils/__tests__/jsonUtils.test.ts b/client/src/utils/__tests__/jsonUtils.test.ts index 2a580f4..055e1df 100644 --- a/client/src/utils/__tests__/jsonUtils.test.ts +++ b/client/src/utils/__tests__/jsonUtils.test.ts @@ -149,17 +149,17 @@ describe("updateValueAtPath", () => { }); test("initializes an empty object when input is null/undefined and path starts with a string", () => { - expect(updateValueAtPath(null as any, ["foo"], "bar")).toEqual({ + expect(updateValueAtPath(null, ["foo"], "bar")).toEqual({ foo: "bar", }); - expect(updateValueAtPath(undefined as any, ["foo"], "bar")).toEqual({ + expect(updateValueAtPath(undefined, ["foo"], "bar")).toEqual({ foo: "bar", }); }); test("initializes an empty array when input is null/undefined and path starts with a number", () => { - expect(updateValueAtPath(null as any, ["0"], "bar")).toEqual(["bar"]); - expect(updateValueAtPath(undefined as any, ["0"], "bar")).toEqual(["bar"]); + expect(updateValueAtPath(null, ["0"], "bar")).toEqual(["bar"]); + expect(updateValueAtPath(undefined, ["0"], "bar")).toEqual(["bar"]); }); // Object update tests @@ -293,10 +293,8 @@ describe("getValueAtPath", () => { }); test("returns default value when input is null/undefined", () => { - expect(getValueAtPath(null as any, ["foo"], "default")).toBe("default"); - expect(getValueAtPath(undefined as any, ["foo"], "default")).toBe( - "default", - ); + expect(getValueAtPath(null, ["foo"], "default")).toBe("default"); + expect(getValueAtPath(undefined, ["foo"], "default")).toBe("default"); }); test("handles array indices correctly", () => { diff --git a/client/src/utils/jsonUtils.ts b/client/src/utils/jsonUtils.ts index 0987859..28cbf30 100644 --- a/client/src/utils/jsonUtils.ts +++ b/client/src/utils/jsonUtils.ts @@ -25,10 +25,19 @@ export type JsonSchemaType = { export type JsonObject = { [key: string]: JsonValue }; -const typeofVariable = typeof "random variable"; -export function getDataType( - value: JsonValue, -): typeof typeofVariable | "array" | "null" { +export type DataType = + | "string" + | "number" + | "bigint" + | "boolean" + | "symbol" + | "undefined" + | "object" + | "function" + | "array" + | "null"; + +export function getDataType(value: JsonValue): DataType { if (Array.isArray(value)) return "array"; if (value === null) return "null"; return typeof value;