Skills2026年4月11日·1 分钟阅读

tRPC — End-to-End Typesafe APIs for TypeScript

tRPC lets you build fully typesafe APIs without schemas, code generation, or runtime bloat. Share types between frontend and backend automatically. Works with Next.js, React, Vue, and more.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
先审查命令
npx -y tokrepo@latest install baa87f69-3559-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
Build fully typesafe APIs that share types between frontend and backend automatically. No schemas, no codegen, no runtime bloat.
§01

What it is

tRPC lets you build fully typesafe APIs in TypeScript without writing schemas, running code generators, or adding runtime validation overhead. When you define a procedure on the server, the client automatically knows the input types, output types, and available endpoints. Change a return type on the server and your frontend shows a compile error immediately.

tRPC targets TypeScript teams building full-stack applications where the API layer between frontend and backend is a source of type mismatches. It works with Next.js, React, Vue, and any TypeScript frontend framework.

§02

How it saves time or tokens

tRPC eliminates the schema definition and code generation step that tools like GraphQL or OpenAPI require. There is no .graphql file to maintain, no openapi.yaml to keep in sync, and no codegen CLI to run after changes. TypeScript's type inference handles everything at compile time. For development velocity, this means changing an API response shape and seeing the impact across your entire codebase instantly.

§03

How to use

  1. Install tRPC packages:
npm install @trpc/server @trpc/client @trpc/react-query @tanstack/react-query zod
  1. Define a router on the server:
import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();

export const appRouter = t.router({
  getUser: t.procedure
    .input(z.object({ id: z.string() }))
    .query(({ input }) => {
      return { id: input.id, name: 'Alice', role: 'admin' };
    }),
});

export type AppRouter = typeof appRouter;
  1. Call from the client with full type safety -- the client knows getUser returns { id, name, role }.
§04

Example

Full client-server type inference:

// Client-side: types are inferred from the server router
import { trpc } from './utils/trpc';

function UserProfile() {
  const { data } = trpc.getUser.useQuery({ id: '123' });
  // data is typed as { id: string; name: string; role: string }
  return <h1>{data?.name}</h1>;
}

Rename name to displayName on the server, and the client shows a TypeScript error immediately.

§05

Related on TokRepo

§06

Common pitfalls

  • tRPC requires both frontend and backend to be TypeScript. If your backend is Python, Go, or another language, tRPC does not apply.
  • tRPC uses HTTP POST for all procedures by default. This can cause issues with CDN caching that expects GET requests. Use the httpBatchLink configuration for batching.
  • Large routers with hundreds of procedures can slow down TypeScript type checking. Split routers into smaller sub-routers and merge them.

常见问题

Does tRPC replace GraphQL?+

For TypeScript-only stacks, tRPC provides similar type safety benefits without the schema layer. If you need a language-agnostic API consumed by non-TypeScript clients, GraphQL or REST with OpenAPI is more appropriate.

What is the runtime overhead of tRPC?+

Minimal. tRPC's type safety is entirely at compile time. At runtime, it is a thin layer over HTTP that serializes and deserializes JSON. There is no runtime schema validation unless you explicitly add Zod input validation.

Does tRPC work with Next.js?+

Yes. tRPC has first-class Next.js integration. The server router runs in Next.js API routes, and the client hooks work in React components. This is the most common deployment pattern for tRPC.

Can I use tRPC with React Query?+

Yes. The @trpc/react-query package wraps TanStack React Query, providing familiar useQuery and useMutation hooks with full tRPC type inference. You get caching, refetching, and optimistic updates out of the box.

How does tRPC handle authentication?+

tRPC uses middleware and context. You create a context function that extracts the user from the request (JWT, session, etc.) and pass it to procedures. Protected procedures check the context for authentication before executing.

引用来源 (3)
  • tRPC GitHub— tRPC provides end-to-end typesafe APIs for TypeScript
  • tRPC Documentation— Type inference between frontend and backend without code generation
  • tRPC Next.js Docs— Next.js integration for full-stack TypeScript applications

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产