Repository navigation
Description
Provide environment information
System:
OS: Windows 11 10.0.26100
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz
Memory: 18.28 GB / 31.87 GB
Binaries:
Node: 22.11.0 - C:\Program Files\nodejs\node.EXE
npm: 11.3.0 - C:\Program Files\nodejs\npm.CMD
pnpm: 8.9.2 - ~\AppData\Local\pnpm\pnpm.EXE
bun: 1.2.16 - ~.bun\bin\bun.EXE
Browsers:
Edge: Chromium (135.0.3179.66)
Internet Explorer: 11.0.26100.1882
npmPackages (server):
@trpc/server: ^11.4.2 => 11.4.2
typescript: ^5.8.3 => 5.8.3
npmPackages (client):
@trpc/client: ^11.4.2 => 11.4.2
@trpc/server: ^11.4.2 => 11.4.2
typescript: ^5.8.3 => 5.8.3
Describe the bug
As discussed with a maintainer in Discussion #6829, I'm opening this issue to report a type-leakage bug.
When a server-only type (like an instance of a pino logger) is included in the tRPC Context, its type definition leaks to the client when importing the AppRouter. This causes TypeScript compilation to fail on the client-side because it cannot resolve the server-only dependency.
The expected behavior is that the client-side type inference for AppRouter should not be affected by non-serializable, server-only objects within the Context. The maintainer in the discussion confirmed that the context is not supposed to leak, which suggests this is unintended behavior.
The primary errors observed on the client are:
- A type portability error
ts(2742)on thecreateTRPCClientcall, explicitly mentioning the server'spinomodule. - A type constraint failure on the
AppRoutergeneric, indicating a module path mismatch between the server's and client's versions of@trpc/serverdue to the polluted context type. (Not visible in repro repo)
Link to reproduction
To reproduce
- Clone the reproduction repository:
git clone https://github.com/matvejs16/trpc-type-leak.git - Navigate into the project directory:
cd trpc-type-leak - Install all dependencies in
clientandserver:npm install - Open the project in a TypeScript-aware editor like VS Code.
- Navigate to the file:
client/src/tRPC.ts. - You will immediately see TypeScript errors highlighted on the
clientvariable declaration, demonstrating the type-leak issue. No need to run the application, as this is a compile-time/type-checking error.
Additional information
The core of the issue seems to be that the inferred type of AppRouter includes the full, non-portable type definition of the Context object. In the reproduction, the Context contains a requestLogger property typed as pino.Logger. Because the client package does not have pino as a dependency, TypeScript rightfully fails to resolve the type.
This issue is likely to affect anyone working in a monorepo who attempts to integrate common server-side tools like loggers, database clients, or other class-based services directly into the tRPC context.
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR fixing this bug!
Activity
KATT commentedon Jun 24, 2025
I've looked into this now & I was wrong earlier - I now think it's unavoidable to have some "leak" like this in the server and it shouldn't be a problem, unless you'd want to publish your backend on npm.
The core of the problem is your monorepo setup - for inspiration you can look at t3-turbo or any of the many monorepo examples on /awesome
github-actions commentedon Jun 25, 2025
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.
KATT commentedon Jun 27, 2025
I created a monorepo example using npm here https://github.com/trpc/examples-npm-monorepo