📡

Understanding tRPC for Typesafe APIs

Apr 22, 2025

Lecture Notes: tRPC Overview

Introduction to tRPC

  • Purpose: Simplifies building & consuming typesafe APIs without schemas or code generation.
  • Trend: Increasing use of TypeScript and static typing in web development.
  • Problem Addressed: Statically typing API endpoints and sharing types between client and server.
  • Solution: A library that utilizes TypeScript for building typesafe APIs.

Alternative to REST or GraphQL

  • Current Dominance: GraphQL is prevalent for implementing typesafe APIs in TypeScript.
  • Limitation of GraphQL: It's language-agnostic and does not fully leverage TypeScript's capabilities.
  • Advantage of tRPC for TypeScript Projects: Direct type sharing between client and server without code generation.

Target Audience for tRPC

  • Who: Full-stack developers using TypeScript.
  • Benefits: Facilitates writing endpoints usable in both frontend and backend.
  • Error Handling: Catches type errors at build time, reducing runtime bugs.

Key Features of tRPC

  • Testing & Readiness: Well-tested and ready for production.
  • Typesafety & Autocompletion: Complete typesafety and autocompletion for client-side inputs, outputs, and errors.
  • Developer Experience (DX):
    • No need for code generation or additional run-time processes.
    • Minimal build pipeline requirements.
  • Lightweight: Zero dependencies and a small client-side footprint.
  • Flexibility:
    • Suitable for new projects and easy integration into existing (brownfield) projects.
    • Framework agnostic with community-built adapters for popular frameworks.
  • Additional Features:
    • Supports subscriptions for typesafe observability.
    • Request batching to combine concurrent requests.

Learning and Examples

  • Example Applications: Resources available for learning and application starting points.