#1370·peerjs

`Peer`'s constructor overloads can't express "no id, but here are options"

Author: tophemanCreated Sep 21, 2026Updated Sep 21, 2026

The implementation accepts an absent id alongside options, and the doc comment says so, but no declared overload admits it. A caller holding an id of type string | undefined plus a config object has to cast.

The declarations

lib/peer.ts (current master, and 1.5.5 as shipped):

typescript
constructor();
constructor(options: PeerOptions);
constructor(id: string, options?: PeerOptions);

constructor(id?: string | PeerOptions, options?: PeerOptions) {

The implementation signature already has the shape callers need — it just isn't public. The three overloads have been unchanged since a43193fb (2022-05-13).

The runtime already supports it

typescript
let userId: string | undefined;

// Deal with overloading
if (id && id.constructor == Object) {
  options = id as PeerOptions;
} else if (id) {
  userId = id.toString();
}

A falsy first argument leaves userId undefined and leaves the second argument as options, so the peer is constructed with the caller's options and the brokering server allocates the id. Further down, if (userId) this._initialize(userId); else this._api.retrieveId()... confirms it. The doc comment on the third overload says the same thing in words: "If no ID is given, one will be generated by the brokering server."

What it looks like from outside

typescript
import { Peer, type PeerOptions } from "peerjs";

declare const storedId: string | undefined; // e.g. read back from sessionStorage
declare const options: PeerOptions;

new Peer(storedId, options);
//       ~~~~~~~~
// TS2345: Argument of type 'string | undefined' is not assignable to parameter
//         of type 'string'.
//   Type 'undefined' is not assignable to type 'string'.

Without the options argument it is TS2769 instead, since both of the one-argument overloads are candidates:

typescript
new Peer(storedId);
// TS2769: No overload matches this call.
//   Overload 1 of 3, '(options: PeerOptions): Peer', gave the following error.
//     Argument of type 'string | undefined' is not assignable to parameter of type 'PeerOptions'.
//   Overload 2 of 3, '(id: string, options?: PeerOptions | undefined): Peer', gave the following error.
//     Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

"Reuse my id if I have one, otherwise let the server pick" is the ordinary shape of any app that persists its peer id across reloads, and anyone doing that with a non-default host, port, path or config lands here on their first line of TypeScript.

The two ways out are both worse than the problem. Branching at runtime so each arm hits a declared overload turns a gap in the types into code that ships:

typescript
const peer = storedId ? new Peer(storedId, options) : new Peer(options);

And casting pushes the same cast onto every call site, or into a wrapper module that then has to be re-exported and documented. We ended up with the latter, in common-peerjs.ts:

typescript
export const Peer = PeerJs as typeof PeerJs & {
  new (id: string | undefined, options?: PeerOptions): PeerJs;
};

Proposal

Widen the third overload's id:

diff
-constructor(id: string, options?: PeerOptions);
+constructor(id: string | undefined, options?: PeerOptions);

It describes what the implementation already does, needs no runtime change, and is backwards compatible: widening a parameter can't reject a call that used to compile, new Peer() still resolves against the first overload, and new Peer(options) still resolves against the second — PeerOptions is not assignable to string | undefined, so the new form doesn't capture it.

Checked against 1.5.5's types.d.ts with the one-line change applied: new Peer(), new Peer(options), new Peer(id), new Peer(id, options), new Peer(storedId) and new Peer(storedId, options) all resolve, under strict.

Happy to open a PR.

Versions

I searched open and closed issues, pull requests and discussions before filing and found nothing covering this; the nearest neighbour is #1335, a different symbol but the same genre of declarations disagreeing with the runtime.

Reported against peerjs 1.5.5; master carries the same three overloads as of 2026-09-21. Observed with TypeScript 6.0.3 — any version with strictNullChecks on will do, since without it undefined is assignable to string and the call compiles.