Skip to content

getAgentByName stub RPC methods return type never #765

@alexander-zuev

Description

@alexander-zuev

Description

When using getAgentByName() to get an Agent stub, calling RPC methods returns type never instead of the actual return type. Accessing properties on the result causes TypeScript errors.

Reproduction

import { Agent, getAgentByName } from 'agents'

interface MyState {
  count: number
}

class MyAgent extends Agent<Env, MyState> {
  async myMethod(): Promise<{ data: string }> {
    return { data: 'hello' }
  }
}

// In worker:
const agent = await getAgentByName(env.MY_AGENT, 'id')
const result = await agent.myMethod()  // result: never
console.log(result.data)  // ❌ Property 'data' does not exist on type 'never'

Expected behavior

result should be typed as { data: string }

Workaround

Passing the result directly to c.json({ data: result }) works because it doesn't access properties. But any property access fails.

Environment

  • agents: 0.3.3
  • @cloudflare/workers-types: ^4.20251231.0
  • wrangler: ^4.54.0

Root cause hypothesis

The Rpc.DurableObjectBranded type brand isn't properly flowing through the type inheritance chain from DurableObjectServerAgent → user's class, causing DurableObjectStub<T> to resolve methods as returning never.

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