TypeScript types error using zod.

Good Evening,

I have run a few different versions of the below fetch and parse of the json, I am recieving an error of "Unsafe return of an any[] type value" with both of these functions, I'm not sure what I am doing wrong. I also included my zod schemas to see if maybe thats where the problem is. I appreciate any and all help


export async function fetchProfessors(): Promise<ProfessorArray> {
const response = await fetch"API_URL/professors");
const professors: ProfessorArray = [];
const data = (await response.json()) as ProfessorArray;
data. forEach ((item) => {
const result = ProfessorSchema. safeParse (item);
if (!result.success) {
console. log (result.error);
} else {
professors. push(result.data)
}
}) ;
return professors;
}

export async function fetchCourses(): Promise<CourseArray> {
const response = await fetch"API_URL/courses");
const data: unknown = await response. json;
const result = CourseArraySchema.safeParse(data) ;
if (!result.success) {
throw new Error("not course array");
}
return result.data;

export const AvgMetrics = z. object({
difficulty: z. number(), value: z. number, hours_per_week: z. number),
average_grade: z.string(),
});

export const CourseSchema: z.ZodSchema = z. lazy(() => z. object({
id: z. number(),
course_id: z.string(),
name: z. string(),
professors: ProfessorSchema, 
courseMetrics: AvgMetrics,
),
});

export const ProfessorReviews = z. object ({
id: z. number,
overall: z. number@,
knowledge: z. number(),
preparation: z. number(),
helpfulness: z. number(),
professor: z. number(),
comment: z.string().nullable(),
});

export const ProfessorSchema: z.ZodSchema = z. lazy (() => z.object({
id: z. number,
name: z. stringO, 
courses: CourseSchema,
reviews: ProfessorReviews,
),
});

export const CourseArraySchema = z. array (CourseSchema);
export const ProfessorArraySchema = z. array(ProfessorSchema);
export type ProfessorArray = z.infer<typeof ProfessorArraySchema>;
export type CourseArray = z. infer<typeof CourseArraySchema>;
export type Course = z. infer<typeof CourseSchema>;
export type ProfessorReviews = z.infer<typeof ProfessorReviews>;
export type Professor = z. infer<typeof ProfessorSchema>;
export type AvgMetrics = z. infer<typeof AvgMetrics>;
Was this page helpful?