Skip to main content
Version: 10.x

常见问题解答/故障排除

常见问题的收集以及有关如何排除故障和解决这些问题的想法。

¥Collection of frequently asked questions with ideas on how to troubleshoot & resolve them.

如果你有此处未解答的问题,请随时为此页面做出贡献以进行改进或创建新的讨论 在 GitHub 上。另外,如果此处没有解答你的问题,请查看 GitHub 讨论 和我们的 Discord

¥Feel free to contribute to this page with improvements or create a new discussion on GitHub if you have a question that isn't answered here. Also, have look through the GitHub Discussions and our Discord if your question isn't answered here.

这不起作用!我到处都是 any

¥It doesn't work! I'm getting any everywhere

  • 确保代码中没有类型错误

    ¥Make sure you have no type errors in your code

  • 确保你的 tsconfig.json 中有 "strict": true

    ¥Make sure you have "strict": true in your tsconfig.json

  • 确保你的 @trpc/* 版本与 package.json 匹配

    ¥Make sure your @trpc/*-versions match in your package.json

如何让中间件更改我的 Context 的类型?

¥How do I make a middleware change the type of my Context?

参见 上下文扩展

¥See Context Extension.

tRPC 生产准备好了吗?

¥Is tRPC production ready?

是的。tRPC 非常稳定,被数千家公司使用,甚至像 Netflix普莱奥 这样的大公司也在生产中使用 tRPC。

¥Yes. tRPC is very stable and is used by thousands of companies, even big ones like Netflix & Pleo are using tRPC in production.

为什么 tRPC 在我的 monorepo 中不起作用?

¥Why doesn't tRPC work in my monorepo?

这是一个很难回答的问题,但由于 tRPC 没有任何构建步骤,因此问题不太可能出在 tRPC 方面。

¥This is difficult question to answer, but since tRPC doesn't have any build step, it's unlikely that the problem is on tRPC's side.

以下是一些需要检查的事项:

¥Here are some things to check:

  • 确保你的所有项目中的所有 @trpc/* 版本均相同

    ¥Make sure you have the same version of all @trpc/* across all your project

  • 确保你的所有 tsconfig.json 中都有 "strict": true

    ¥Make sure you have "strict": true in all your tsconfig.jsons

  • 确保你的应用中没有类型错误

    ¥Make sure you have no type errors in your app

  • 如果你有专用服务器和客户端 tsconfig.json 文件,而没有打包服务器 monorepo 包,请确保你的客户端 tsconfig.json 中有 "paths": [...],就像你的服务器 tsconfig.json 一样,以便客户端可以找到相同的文件。

    ¥In the case that you have a dedicated server and client tsconfig.json files without a bundled server monorepo package, make sure you have "paths": [...] in your client tsconfig.json like your server tsconfig.json,so that the client can find the same file.

你还可以查看我们的 很棒的 tRPC-collection,找到几个在 monorepo 中使用 tRPC 的开源项目。

¥You can also have a look at our Awesome tRPC-collection to find several open-source projects that are using tRPC in a monorepo.

monorepo 是强制性的吗?

¥Is a monorepo mandatory?

不,monorepo 不是强制性的,但如果你不使用 tRPC,你将失去使用它的一些好处,因为你将失去客户端和服务器协同工作的保证。

¥No, a monorepo is not mandatory but you will lose some of the benefits of using tRPC if you don't use it since you will lose guarantees that your client and server works together.

利用 tRPC 的一种方法是发布具有后端存储库类型的私有 npm 包,并在前端存储库中使用它们。

¥One way you can leverage tRPC is to publish a private npm package with the types of your backend repo and consume them in your frontend repo.

相关讨论:https://github.com/trpc/trpc/discussions/1860

¥Related discussion: https://github.com/trpc/trpc/discussions/1860

我可以根据发送的输入动态返回不同的输出吗?

¥Can I dynamically return a different output depending on what input I send?

不,目前还不行,为了让 tRPC 自动执行此操作,我们需要名为 "高等种类" 的东西,TypeScript 尚不支持它。

¥No, not currently, in order for tRPC to do that automatically, we need something called "Higher kinded types" which is not yet supported in TypeScript.

相关讨论:https://github.com/trpc/trpc/discussions/2150

¥Related discussion: https://github.com/trpc/trpc/discussions/2150

我可以将中间件应用于完整的路由吗?

¥Can I apply a middleware to a full router?

不,但你可以使用 基本过程 来代替,这比在每个路由级别上完成此操作提供了更大的灵活性。

¥No, but you can use base procedures instead, which offers more flexibility than if this was done on a per-router-level.

tRPC 是否可以与 Next.js 13 应用布局和 RSC 配合使用?

¥Does tRPC work with Next.js 13 App Layouts & RSC?

是的,tRPC 可与 Next.js 13 应用布局和 React 服务器组件配合使用,但我们尚未构建任何官方示例。

¥Yes, tRPC works with Next.js 13 App Layouts & React Server Components, but we have not built any official examples of it yet.

有关更多信息,你可以阅读并关注 这个问题,我们在其中引用了一些示例。

¥For more information, you can read & follow this issue where we've referenced a few examples of it.

使用标记为 unstable_ 的功能安全吗?

¥Am I safe with using features marked as as unstable_?

tl;dr:是的!

¥tl;dr: Yes!

如果你在 tRPC 中遇到标记为 unstable_ 的功能,则意味着该 API 不稳定并且可能会在次要版本更新中发生变化,但是:

¥If you encounter a feature in tRPC that is marked as unstable_ it means that the API is unstable and might change in minor version bumps, but:

  • 实现细节可能会发生细微的变化 - 它的名称和选项可能会改变

    ¥Specifics of the implementation might change in minor changes - its name and options might change

  • 如果它存在于 tRPC 中,则它已经在生产中使用了

    ¥If it exists in tRPC it's already used it in production

  • 我们非常鼓励你使用它

    ¥We very much encourage you to use it

  • 如果对 unstable_ 功能进行任何更改,它们将包含在发行说明中(你将看到类型错误)

    ¥If any changes are done to unstable_-feature, they will be included in the release notes (& you'll see type errors)

  • 请在 github.com/trpc/trpc/issues我们的 Discord#🧪-unstable-experimental-features 中报告有关 API 设计或问题的任何建议

    ¥Please report any suggestion on the API design or issues on github.com/trpc/trpc/issues or in the #🧪-unstable-experimental-features on our Discord

使用标记为 experimental_ 的功能安全吗?

¥Am I safe with using features marked as as experimental_?

如果你在 tRPC 中遇到标记为 experimental_ 的功能,则意味着该 API 不稳定,并且很可能在 tRPC 的任何波动期间发生变化。

¥If you encounter a feature in tRPC that is marked as experimental_ it means that the API is unstable and is very likely to change during any bump of tRPC.

  • 该功能范围广泛,其用法可能会发生变化

    ¥Wide range of the feature and its usage might change

  • 该功能可能没有经过充分测试

    ¥The feature might not be well-tested

  • 我们可能会完全放弃该功能

    ¥We might drop the feature entirely

  • 你可以自行阅读最新文档并进行升级,无需指导迁移路径

    ¥It's up to you to read the latest docs and upgrade without a guided migration path

  • 发行说明中可能没有详细记录更改

    ¥Changes might not be well-documented in the release notes

  • 不保证修复错误

    ¥Bugs are not guaranteed to be fixed

然而,我们确实喜欢输入!请在 我们的 Discord#🧪-unstable-experimental-features 中报告任何有关 API 设计的建议或问题。

¥We do, however, love input! Please report any suggestion on the API design or issues in the #🧪-unstable-experimental-features on our Discord.

tRPC 对 semver 严格吗?

¥Is tRPC strict with semver?

是的,tRPC 对 语义版本控制 非常严格,我们永远不会在次要版本升级中引入重大更改。

¥Yes, tRPC is very strict with semantic versioning and we will never introduce breaking changes in a minor version bump.

这样,除了 JSDoc 中标记为 @internal 的更改之外,我们还将 export TypeScript type 的更改视为主要更改。

¥With this, we also consider changes on exported TypeScript types as major changes, apart from ones marked as @internal in the JSDoc.

为什么 tRPC 已经是这么高的版本了?

¥Why is tRPC on such a high version already?

当 tRPC 启动并且用户很少时,我们经常迭代 API 设计,同时严格遵守 semver。

¥When tRPC started and had very few users and we often iterated on the API design whilst being strict with semver.

  • tRPC 的前 9 个版本在项目的前 8 个月内发布。

    ¥The first 9 versions of tRPC were released in the first 8 months of the project.

  • 我们在 v9 后 14 个月发布的 版本 10 应该被视为 tRPC 的真正 "版本 2",我们对 API 决策做了任何根本性的改变。(2 is 10 in binary, amirite?)

    ¥Version 10 which we released 14 months after v9 should be seen as the real "version 2" of tRPC where we did any fundamental changes to the API decisions. (2 is 10 in binary, amirite?)

我们预计 API 现在会稳定,并计划针对未来的任何重大更改发布代码模块,就像我们对 v9->v10 升级所做的那样。

¥We expect the API to be stable now and are planning to release codemods for any breaking changes in the future, just like we did with the v9->v10 upgrade.


你还有什么想知道的吗?

¥Anything else you want to know?

请在 GitHub 上写功能请求,在 GitHub 讨论Discord 上写。你还可以使用页面底部的 "编辑这个页面" 按钮随意提出对此页面或任何其他页面的改进建议。

¥Please write a feature request on GitHub, write in GitHub Discussions, or Discord. You're also free to suggest improvement of this page or any other page using the "Edit this page" button at the bottom of the page.