The problem
The platform had accumulated three years of any casts, partial types, and hand-rolled validators that diverged from the database schema on every migration. A runtime error in production was not an anomaly — it was a scheduled event.
The approach
Rather than patching the existing type layer, we replaced it. The architecture established a single source of truth at the database schema level, with inference flowing outward into the application layer.
// Schema as the single source of truth
const ArticleSchema = z.object({
id: z.string().uuid(),
title: z.string().min(1).max(280),
body: z.string(),
publishedAt: z.date().nullable(),
authorId: z.string().uuid(),
});
type Article = z.infer<typeof ArticleSchema>;
The key constraint: no manual type definitions for domain entities. Every shape is inferred. The compiler is the validator.
Results
- Zero runtime type errors in the six months following deployment
- Onboarding time for new engineers reduced from two weeks to three days — the types describe the domain
- Migration-time schema divergence eliminated entirely