79138219

Date: 2024-10-29 17:00:05
Score: 1
Natty:
Report link

For anyone in the future who is curious, I ended up doing the type inference as part of parsing the response (with zod in this case).

export async function getUsers(): Promise<User[]> {
  const res = await api.get("/users");
  const parsed = userSchema.array().safeParse(res);

  if (!parsed.success) {
    throw new Error(`Users "${res}" is not valid.`);
  }

  return parsed.data;
}

Happy coding!

Reasons:
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Amr Gaber