birpc icon indicating copy to clipboard operation
birpc copied to clipboard

Ensure second type parameter 'LocalFunctions' is optional

Open iSuslov opened this issue 1 month ago • 0 comments

Description

This PR resolves a TypeScript issue that occurred when developers called createBirpc or createBirpcGroup with only one type parameter.

Previously, this would cause a TypeScript error:

import { createBirpc } from "birpc";

const serverFunctions = { hiFromServer: () => `Hi from server` };
const clientFunctions = { hiFromClient: () => `Hi from client` };

const birpc = createBirpc<typeof serverFunctions>(
  clientFunctions, // <--- Error: Argument of type '{ hiFromClient(): string; }' is not assignable to parameter of type 'Record<string, never>'.
                   //  Property 'hiFromClient' is incompatible with index signature.
                   //  Type '() => string' is not assignable to type 'never'. TS(2345)
  {
    on(fn) {},
    post(data, ...extras) {},
  }
);

It is now possible to provide only one type parameter without error. This change does not affect the developer experience (DX) unless typed access to birpc.$functions is required. In that scenario, providing both type parameters will still enable full type checking for birpc.$functions.

Linked Issues

https://github.com/antfu-collective/birpc/issues/32

Additional Context

This fix resolves the main example in the README.md, which was previously broken:

import type { ServerFunctions } from './types'

const ws = new WebSocket('ws://url')

const clientFunctions: ClientFunctions = {
  hey(name: string) {
    return `Hey ${name} from client`
  }
}

const rpc = createBirpc<ServerFunctions>(
  clientFunctions,
  {
    post: data => ws.send(data),
    on: fn => ws.on('message', fn),
    // these are required when using WebSocket
    serialize: v => JSON.stringify(v),
    deserialize: v => JSON.parse(v),
  },
)

await rpc.hi('Client') // Hi Client from server

iSuslov avatar Nov 08 '25 16:11 iSuslov