My goal in this coding playground is to create a versatile API handler helper that guarantees standard response types and also utilizes inference to ensure our application code can effectively handle all potential scenarios:
However, I'm encountering issues with the inference failing to correctly deduce these generics. Does anyone have any suggestions on how to reorganize this to improve the inference process? Thank you!
import { Jsonifiable, AsyncReturnType } from "type-library";
import z, { ZodTypeAny } from "zod";
...
// These outcomes turn out to be too general
// Expecting {foo: "bar"} but receiving Jsonifiable
type successResponse = Extract<AsyncReturnType<typeof apiHandler>, {success: true}>
type errorResponse = Extract<AsyncReturnType<typeof apiHandler>, {success: false}>
type code = errorResponse["code"]; // Hoping for "too big" | "too small", but getting a string