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

Inferring Query Input and Output Types

How the client automatically knows query parameter shapes and return types from server definitions.

Inferring Query Input and Output Types

What you'll learn: How tRPC automatically infers the exact shape of query parameters and return values from your server-side procedure definitions, eliminating manual type duplication.

The Magic of Automatic Type Flow

When you define a tRPC query procedure on your server, something remarkable happens: the client automatically knows what parameters that query accepts and what type of data it returns. You don't write types twice—you define them once on the server, and TypeScript propagates them to every client call site.

This works because tRPC's AppRouter type captures the complete shape of all your procedures. When you create a typed client, TypeScript extracts the input validator schema and output type from each procedure definition and makes them available through the client API.

How It Works in Practice

On the server, you define a query with input validation:

import { z } from 'zod';

const appRouter = router({
  getUser: query({
    input: z.object({
      userId: z.string()
    }),
    resolve({ input }) {
      // Returns a user object
      return {
        id: input.userId,
        name: 'Alice',
        age: 30
      };
    }
  })
});

On the client, TypeScript knows the input shape ({ userId: string }) and output shape ({ id: string; name: string; age: number }) without you writing them again:

// TypeScript knows this requires { userId: string }
const user = await client.getUser.query({ userId: '123' });

// TypeScript knows 'user' has properties: id, name, age
console.log(user.name); // ✓ Autocomplete works!
console.log(user.email); // ✗ Type error: Property 'email' does not exist

The Contract Flows Through Types

Think of your server procedures as a contract written in TypeScript. The AppRouter type is like a detailed specification document. When the client imports this type, it's reading that contract and enforcing it at every call site. Wrong parameter shape? Type error. Accessing a property that doesn't exist on the return value? Type error.

This is end-to-end type safety without code generation, API mocking, or manual synchronization.

Key takeaway: tRPC infers both query input requirements and return types directly from server procedure definitions, giving you autocomplete and type checking on the client without writing types twice.