The Router as Contract
What you'll learn: How the tRPC router simultaneously serves as your runtime implementation and compile-time type contract, eliminating the gap between backend and frontend.
The Dual Nature of a tRPC Router
In traditional API architectures, you maintain three separate artifacts: the runtime implementation (your backend code), the type definitions (TypeScript interfaces), and the contract specification (OpenAPI/Swagger docs). These inevitably drift apart.
tRPC collapses these three into one single definition: the router itself.
Runtime Implementation
When your server runs, the router is actual executable code. It processes requests, runs validation, executes business logic, and returns responses. It's not a type-only abstraction—it's real Node.js code handling real HTTP requests.
Compile-Time Type Contract
At the same time, TypeScript extracts the type signature of every procedure in your router. The client sees these extracted types at compile time. When you call a procedure from the frontend, TypeScript knows exactly what input it expects and what output it will return—all inferred directly from the router definition.
The Single Source of Truth
Because the router IS both the implementation and the contract, they cannot drift apart. Change a procedure's return type? The frontend immediately sees the new type. Add a required input field? The client code won't compile until updated. Rename a procedure? TypeScript catches every call site instantly.
This isn't code generation magic—it's pure TypeScript type inference flowing across the module boundary. The router exports its shape as a type, and the tRPC client consumes that type to provide full autocomplete and type-checking.
Key Takeaway: The tRPC router is simultaneously executable runtime code and a compile-time type contract, making API type-safety automatic and impossible to violate without detection.