Skip to content

Private class fields cause type incompatibility with duplicate package instances #126

@chazcb

Description

@chazcb

Problem

AgentSideConnection uses native JS #private fields, which breaks TypeScript structural typing when multiple copies of the package exist in node_modules.

In monorepos using pnpm, it's common for @agentclientprotocol/sdk to be installed multiple times with different peer dependency versions (e.g., zod@4.2.1 vs zod@4.3.6). Even though the code is identical, TypeScript treats #private fields from different package instances as incompatible:

error TS2345: Argument of type 'AgentSideConnection' is not assignable to parameter of type 'AgentSideConnection'.
  Property '#private' in type 'AgentSideConnection' refers to a different member that cannot be accessed from within type 'AgentSideConnection'.

This is a well-known footgun with native #private fields in library code — see TypeScript #48885.

Suggested Fix

Use TypeScript's private keyword instead of native #private fields for classes that are part of the public API (like AgentSideConnection and ClientSideConnection). The private keyword is erased at compile time and doesn't break structural typing across duplicate instances.

Workaround

Consumers can work around this by ensuring a single copy of the package via pnpm overrides or by aligning all peer dependency versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions