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 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>;
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>;
1 Reply
Captainrobb
CaptainrobbOP7mo ago
I seem to have finally found something that works though idk how correct it is. I changed the ProfessorSchema to ProfSchema and then created another const ProfessorSchema which extends ProfSchema and then I left CourseSchema lazy loading where it is and it seems typescript is now able to infer the return value
export const ProfSchema = z.object({
id: z.number(),
name: z.string(),
reviews: ProfessorReviews,
});

export const ProfessorSchema = ProfSchema.extend({
courses: CourseSchema
})
export const ProfSchema = z.object({
id: z.number(),
name: z.string(),
reviews: ProfessorReviews,
});

export const ProfessorSchema = ProfSchema.extend({
courses: CourseSchema
})
would still love the opinion of someone more familiar with this than I Bumping this
Want results from more Discord servers?
Add your server