Frontend & Accessibility · Updated 1 min read111 wordsby Maya Patel
Enforcing End-to-End TypeScript Contracts Across REST Boundaries
Stop silent payload drift between Django REST serializers and React views using strict discriminated unions and boundary validation.

GraphQL and bespoke RPC codegen promise type safety, yet most British product teams run clean HTTP REST APIs alongside a TypeScript frontend. Type errors still sneak in when nullable fields change on the backend or error responses lack an explicit contract.
We model every API mutation response as an explicit discriminated union rather than throwing unstructured exceptions across React components:
export type ApiMutationResult<T> =
| { ok: true; data: T }
| { ok: false; status: number; error: string; fieldErrors?: Record<string, string> };
export function parseReferenceResponse(raw: unknown): ApiMutationResult<{ referenceId: string }> {
if (typeof raw === 'object' && raw !== null && 'referenceId' in raw) {
return { ok: true, data: { referenceId: String((raw as { referenceId: unknown }).referenceId) } };
}
return { ok: false, status: 400, error: 'Unexpected API response shape.' };
}Combined with strict TypeScript compiler checks in CI, UI forms handle validation feedback deterministically and regressions fail at build time instead of in a customer browser.


