Skip to content

Allow custom connector classes in the React, Vue, and Svelte wrappers - #554

Merged
taylorotwell merged 2 commits into
laravel:2.xfrom
sebestenyb:fix/wrapper-custom-connector-types
Sep 18, 2026
Merged

taylorotwell merged 2 commits into
laravel:2.xfrom
sebestenyb:fix/wrapper-custom-connector-types

Conversation

@sebestenyb

Copy link
Copy Markdown
Contributor

Problem

The React, Vue, and Svelte wrappers reject custom connector classes at the type level, even though laravel-echo and the wrappers' own runtime support them.

laravel-echo exposes the custom-connector arm through Broadcaster["function"], whose broadcaster is a Constructor<…>, and Echo's own generic is T extends keyof Broadcaster. The wrappers, however, constrain configureEcho() / echo() to BroadcastDriver, which is Exclude<keyof Broadcaster, "function">:

// packages/vue/src/config/index.ts (before)
export const configureEcho = <T extends BroadcastDriver>(config: EchoOptions<T>): void => { … };

So this fails to compile:

import { configureEcho } from "@laravel/echo-vue";
import { WaveConnector } from "laravel-wave";

configureEcho({
    broadcaster: WaveConnector,
});
// error TS2322: Type 'typeof WaveConnector' is not assignable to type
// '"reverb" | "pusher" | "ably" | "socket.io" | "mercure" | "null"'.

Runtime was never the problem: defaults[WaveConnector] is undefined, so the spread is a no-op, and new Echo(config) already accepts a connector class (Echo#connect() has an isConstructor(this.options.broadcaster) branch). Only the wrappers' generic is narrower than the library it wraps, so users have to cast or use laravel-echo directly and give up the framework helpers.

Change

In packages/{react,vue,svelte}/src/config/index.ts:

  • configureEcho, echo, and getEchoInstance are constrained to keyof Broadcaster instead of BroadcastDriver, matching Echo<T extends keyof Broadcaster>.
  • The stored config is typed as Echo's own constructor parameter, type EchoConfig = ConstructorParameters<typeof Echo<keyof Broadcaster>>[0], and built with an explicit new Echo<keyof Broadcaster>(echoConfig). Without the explicit type argument, inference collapses T back to the string drivers (via broadcaster: T | Constructor) and rejects the "function" arm again.
  • The defaults lookup is guarded, since custom connectors have no defaults to merge:
const { broadcaster } = config;

echoConfig = {
    ...(typeof broadcaster === "string" ? defaults[broadcaster] : null),
    ...config,
} as EchoConfig;

The typeof narrowing lands exactly on BroadcastDriver, so the lookup needs no cast.

No runtime behavior changes for the built-in drivers: the same defaults are merged for every string broadcaster, and the previously-undefined lookup for a connector class is now simply skipped.

Tests

Each wrapper gets a case in tests/config.test.ts that configures a custom connector class and asserts the instance is built from it and the passed options survive the merge. Because the test files are covered by tsc --noEmit, these also pin the type-level behavior — on master they fail to compile with the exact TS2322 above.

Verified locally:

  • pnpm typecheck — clean across all four packages.
  • pnpm -r run test — 111 / 63 / 49 / 34 tests pass.
  • pnpm lint — no new errors.
  • Built packages/vue; the emitted declaration is now export declare const configureEcho: <T extends keyof Broadcaster>(config: EchoOptions<T>) => void;.

Docs

Each wrapper README gains a short note under configureEcho showing a custom connector class and stating that no defaults are merged in for it.

Non-goals

useEcho, useChannel, and friends keep TDriver extends BroadcastDriver. Broadcaster["function"]'s channel types are any, so widening those generics would only let callers opt into any channels; it is unrelated to the reported error and left alone.

The React, Vue, and Svelte wrappers constrained configureEcho() and echo()
to BroadcastDriver, which excludes the "function" arm of Broadcaster, so
passing a custom connector class failed with TS2322 even though laravel-echo
and the wrapper runtime both support it.

Widen the wrapper generics to keyof Broadcaster and skip the driver defaults
lookup when the broadcaster is a connector class.
@sebestenyb
sebestenyb changed the base branch from master to 2.x September 18, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants