🌐 Web Development

tRPC

tRPC

TypeScript end-to-end type-safe API. Type inference without schema definitions.

Detailed Explanation

tRPC is an API framework that provides end-to-end type safety in TypeScript projects. Unlike REST or GraphQL, there is no need to define a schema separately; types are automatically inferred from the server code to the client.

Code Example

// server/router.ts import { router, publicProcedure } from './trpc'; import { z } from 'zod'; export const appRouter = router({ getUser: publicProcedure .input(z.object({ id: z.string() })) .query(({ input }) => { return { id: input.id, name: 'Hong Gil-dong' }; // types are inferred automatically }), }); // client usage const user = trpc.getUser.useQuery({ id: '1' }); // user.data is of type { id: string, name: string }

Practical Conversation Example

Frontend

"I find it tedious to define API response types. Every time the backend changes, I have to match them manually."

Backend

"With tRPC, if I change a server function's type, the client throws an error right away. You don't even need API documentation."

Cautions

TypeScript only: Both the client and the server must be TypeScript to enjoy the benefits.

Related Terms

Learn More