@daviddios/xtrpc

中文文档
1.1.0发布于 2025/12/16MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.5万

Export your tRPC router to massively improve language server performance and/or let your users consume your API from a typed SDK

原始使用文档
文档目录17 节
  1. What's the problem?
  2. How xTRPC solves it
  3. Installation
  4. Quick start
  5. 1. Create a config file
  6. 2. Generate your types
  7. 3. Use the generated types
  8. Configuration
  9. Example config file
  10. Example app router
  11. Available options
  12. When to use this
  13. How it works
  14. Known limitations
  15. Contributing
  16. Licence
  17. Acknowledgements

👾 xTRPC

Export clean, lightweight TypeScript types from your tRPC router

Fork of @algora/xtrpc - Enhanced for better performance and tRPC 11 compatibility

What's the problem?

Let's say you've built a tRPC API server and want to use those types in your frontend app (or share them with other projects). The obvious solution is to export the types using TypeScript's declaration file generation with tsc.

But here's what goes wrong:

Your type files become massive

When TypeScript generates a .d.ts file, it includes everything your procedures reference - not just the API inputs and outputs you actually need, but also:

  • Internal utility types
  • Database models
  • Helper functions
  • Every dependency of every dependency

Your type file explodes in size, even though consumers only need to know "here's the procedure name, here's what to send, here's what you get back."

Your internals leak out

Worse, your tRPC Context type (which might contain things like database connections, auth state, and internal services) gets exported too. That's a security risk - you're exposing implementation details that should stay private.

Everything slows down

All those extra types mean your IDE has to work much harder. Autocomplete that should take a second now takes 30-60 seconds. Your editor starts to feel sluggish. Type checking crawls.

How xTRPC solves it

xTRPC takes your router and cleans it up before generating the type file:

  1. Removes implementations - Strips out all the function bodies from your procedures and middleware, so the types they use don't end up in the output
  2. Hides your context - Replaces your Context type with any, keeping your internal types private
  3. Exports just the API shape - Generates a minimal type file with only what consumers need: procedure names, inputs, and outputs

The result? A tiny, fast type file that keeps your implementation details private whilst maintaining complete type safety.

Installation

npm i -D @daviddios/xtrpc

Quick start

1. Create a config file

Create xtrpc.config.json in your project root:

{
  "input": {
    "appRouterFilePath": "src/server/router.ts"
  }
}

See configuration for all available options.

2. Generate your types

npx xtrpc

This creates types/api.d.ts containing your cleaned router types.

3. Use the generated types

// Export the generated type
export { type AppRouter } from "my-server/types/api";

// Use it in your client
export const client = createTRPCClient<AppRouter>({
  // ... your config
});

That's it! Your client now has full type safety without any of the baggage.

Configuration

Create xtrpc.config.json in your project root. Only input.appRouterFilePath is required - everything else has sensible defaults.

Example config file

{
  "$schema": "node_modules/@daviddios/xtrpc/dist/xtrpc.schema.json", // Add this to your config file to get autocomplete and validation in your IDE
  "input": {
    "tsconfigPath": "tsconfig.json",
    "appRouterFilePath": "src/server/router.ts",
    "appRouterTypeName": "AppRouter",
    "routerPaths": ["src/server/**/*.ts", "!src/server/**/*.test.ts"]
  },
  "output": {
    "filePath": "types/api.d.ts"
  },
  "verbose": false
}

Example app router

// This file contains your app router, the path of this file
// should be set in the configuration as `input.appRouterFilePath`
import { initTRPC } from '@trpc/server';

const t = initTRPC.create();

export const appRouter = t.router({
    getUsers: t.procedure
        .output(...)
        .query(...)
});

// This is the exported type of your app router, the name of this
// should be set in the configuration as `input.appRouterTypeName`
export type AppRouter = typeof appRouter;

Available options

OptionTypeDefaultDescription
input.tsconfigPathstring"tsconfig.json"Where to find your TypeScript config
input.appRouterFilePathstring(required)The file that contains your app router
input.appRouterTypeNamestring"AppRouter"The name of your exported app router type
input.routerPathsstring[]-Optional glob patterns to search for routers and procedures - only files under these paths will be included, specifying this can make xtrpc faster
output.filePathstring"types/api.d.ts"Where to save the generated types
verbosebooleanfalseShow detailed output whilst running

When to use this

Monorepos

You have a server package and a client package that need to share types, but you don't want to couple them together with implementation details.

Public APIs

You're building a public API and want to give users a typed SDK without exposing your database schema or internal architecture.

Large routers

Your tRPC router has grown large enough that your IDE is noticeably slow when working with it.

Security-conscious projects

You need to ensure sensitive types (auth context, database models, internal services) never leave your server codebase.

How it works

xTRPC uses ts-morph (a tool for working with TypeScript code) to:

  1. Load the file containing your router
  2. Find your router type definition
  3. Transform it:
    • Replace your Context type with any
    • Replace middleware bodies with simple pass-through functions
    • Remove procedure implementations
  4. Save the cleaned version as a .d.ts file

Your actual server code isn't touched - only the generated type file is modified.

Known limitations

Go to definition jumps to generated file

When you use "Go to Definition" in your IDE, it'll take you to the generated .d.ts file instead of your actual source code. This could potentially be fixed by generating declaration maps, but we haven't tested that yet.

Manual configuration required

You need to tell xTRPC exactly where your router file is - it won't try to find it automatically. This is intentional (for performance reasons), but it does mean a bit of manual setup.

Contributing

Found a bug? Have an idea? Open an issue or pull request on GitHub.

Licence

MIT © Algora PBC.

MIT © David Dios

Acknowledgements

This project is a fork of @algora/xtrpc, rebuilt for improved performance and tRPC 11 support. Thanks to the original authors for the innovative approach to this problem.

版本与发布记录

@daviddios/xtrpc 共发布 3 个版本,首次发布于 2025/12/16;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2025/12/1610 个月前
1.0.02025/12/1610 个月前
0.0.12025/12/1610 个月前

关于 @daviddios/xtrpc 的常见问题

@daviddios/xtrpc 是什么?

Export your tRPC router to massively improve language server performance and/or let your users consume your API from a typed SDK

如何安装 @daviddios/xtrpc?

在项目目录运行 npm install @daviddios/xtrpc,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 xtrpc,可以用 npx xtrpc 直接运行。

@daviddios/xtrpc 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@daviddios/xtrpc 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@daviddios/xtrpc 可以商用吗?

@daviddios/xtrpc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@daviddios/xtrpc 的下载量有多少?

根据 npm 官方下载统计,@daviddios/xtrpc 在 2026/10/12 – 2026/11/10 这 30 天内被下载 14,777 次,日均约 493 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@daviddios/xtrpc 还在维护吗?

最新版本 1.1.0 发布于 2025/12/16(10 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/12/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。