Course contentsShow
← TypeScript
Lesson 1571 of 1,79636. tRPC and End-to-End Type SafetyFree lesson

Creating a Typed tRPC Client

Setting up createTRPCProxyClient or similar with the AppRouter type for full inference.

Creating a Typed tRPC Client

You'll learn how to set up a tRPC client that automatically inherits complete type safety from your backend router definition.

The Full-Circle Type Flow

You've defined your AppRouter on the backend, complete with all your procedures and their input/output types. Now comes the magic: creating a client that knows everything about that router without any manual type definitions.

Think of it like this: your AppRouter is a detailed blueprint. The tRPC client is a smart assistant that reads that blueprint and automatically knows what questions it can ask (which procedures exist), what information it needs to provide (input types), and what answers it will receive (output types).

Setting Up the Typed Client

The most common approach uses createTRPCProxyClient from @trpc/client, which creates a proxy-based client that mirrors your router's structure:

import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from './server/router';

const client = createTRPCProxyClient<AppRouter>({
  links: [
    httpBatchLink({
      url: 'http://localhost:3000/trpc',
    }),
  ],
});

Notice the <AppRouter> type parameter? That single generic argument gives the client complete awareness of your entire API surface. The client now knows what procedures exist, what inputs they expect, and what outputs they return.

Type Inference in Action

When you call procedures through this client, TypeScript automatically infers everything:

// TypeScript knows getUser exists and requires { id: string }
const user = await client.user.getUser.query({ id: '123' });
// user's type is automatically inferred from the procedure's output

// TypeScript knows updateProfile is a mutation with specific input shape
const updated = await client.user.updateProfile.mutate({
  name: 'Alice',
  bio: 'Developer',
});

The type information flows from your router definition through the AppRouter type into your client calls. No manual synchronization needed.

Key Takeaway: By passing your AppRouter type to createTRPCProxyClient, you establish end-to-end type safety where every client call is validated against your backend router definition, with full autocomplete and type inference throughout.