Skip to content

bug: Server-only types in Context leak to the client, causing type portability errors #6830

Closed as not planned

Description

@matvejs16

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:

  1. A type portability error ts(2742) on the createTRPCClient call, explicitly mentioning the server's pino module.
  2. A type constraint failure on the AppRouter generic, indicating a module path mismatch between the server's and client's versions of @trpc/server due to the polluted context type. (Not visible in repro repo)

Link to reproduction

https://github.com/matvejs16/trpc-type-leak

To reproduce

  1. Clone the reproduction repository: git clone https://github.com/matvejs16/trpc-type-leak.git
  2. Navigate into the project directory: cd trpc-type-leak
  3. Install all dependencies in client and server: npm install
  4. Open the project in a TypeScript-aware editor like VS Code.
  5. Navigate to the file: client/src/tRPC.ts.
  6. You will immediately see TypeScript errors highlighted on the client variable 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

  1. KATT commented on Jun 24, 2025

    @KATT
    Member

    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

  2. github-actions commented on Jun 25, 2025

    @github-actions
    Contributor

    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.

  3. locked as resolved and limited conversation to collaborators on Jun 25, 2025
  4. KATT commented on Jun 27, 2025

    @KATT
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions