
tRPC Has a Free Type-Safe API Layer — Never Write REST Endpoints Again
REST APIs Have a Type Gap You change an API response field from userName to name . The backend compiles fine. The frontend compiles fine. Everything breaks at runtime because they disagree on the shape of data. OpenAPI, GraphQL codegen, manual types — all try to solve this. They all add complexity. tRPC: End-to-End Type Safety Without Code Generation tRPC connects your TypeScript backend and frontend with zero code generation. Change a return type on the server, your frontend gets a type error instantly. How It Works Backend: Define procedures import { initTRPC } from ' @trpc/server ' import { z } from ' zod ' const t = initTRPC . create () export const appRouter = t . router ({ getUser : t . procedure . input ( z . object ({ id : z . string () })) . query ( async ({ input }) => { const user = await db . users . findUnique ({ where : { id : input . id } }) return user // TypeScript knows the return type }), createUser : t . procedure . input ( z . object ({ name : z . string (), email
Continue reading on Dev.to React
Opens in a new tab




