Skip to content

Commit 6481ae0

Browse files
committed
improve function type
removes unnecessary type casts
1 parent 04c5cbe commit 6481ae0

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

frontend/src/ts/utils/url-handler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ export function loadTestSettingsFromUrl(getOverride?: string): void {
152152
let de: SharedTestSettings;
153153
try {
154154
const decompressed = decompressFromURI(getValue) ?? "";
155-
const parsed = parseJsonWithSchema(decompressed, TestSettingsSchema);
156-
de = parsed as SharedTestSettings; // Assign after refinement
155+
de = parseJsonWithSchema(decompressed, TestSettingsSchema);
157156
} catch (e) {
158157
console.error("Failed to parse test settings:", e);
159158
Notifications.add(

packages/util/src/json.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { ZodError, ZodIssue, ZodSchema } from "zod";
1+
import { z, ZodError, ZodIssue } from "zod";
22

33
/**
44
* Parse a JSON string into an object and validate it against a schema
55
* @param json JSON string
66
* @param schema Zod schema to validate the JSON against
77
* @returns The parsed JSON object
88
*/
9-
export function parseWithSchema<T>(json: string, schema: ZodSchema<T>): T {
9+
export function parseWithSchema<T extends z.ZodTypeAny>(
10+
json: string,
11+
schema: T
12+
): z.infer<T> {
1013
try {
1114
const jsonParsed = JSON.parse(json) as unknown;
12-
const zodParsed = schema.parse(jsonParsed);
13-
return zodParsed;
15+
// hits is fine to ignore
16+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
17+
return schema.parse(jsonParsed) as z.infer<T>;
1418
} catch (error) {
1519
if (error instanceof ZodError) {
1620
throw new Error(error.issues.map(prettyErrorMessage).join("\n"));

0 commit comments

Comments
 (0)