Skip to content

Commit adcac95

Browse files
Merge pull request #437 from laravel/fix-channel-return-type
Fix channel return type
2 parents ec604b2 + 4d4e9dd commit adcac95

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

packages/react/src/hooks/use-echo.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { echo } from "../config";
44
import type {
55
Channel,
66
ChannelData,
7+
ChannelReturnType,
78
Connection,
89
ModelEvents,
910
ModelPayload,
@@ -157,7 +158,8 @@ export const useEcho = <
157158
/**
158159
* Channel instance
159160
*/
160-
channel: () => subscription.current,
161+
channel: () =>
162+
subscription.current as ChannelReturnType<TDriver, TVisibility>,
161163
};
162164
};
163165

packages/react/src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export type Channel = {
1616
visibility: "private" | "public" | "presence";
1717
};
1818

19+
export type ChannelReturnType<
20+
T extends BroadcastDriver,
21+
V extends Channel["visibility"],
22+
> = V extends "presence"
23+
? Broadcaster[T]["presence"]
24+
: V extends "private"
25+
? Broadcaster[T]["private"]
26+
: Broadcaster[T]["public"];
27+
1928
export type ConfigDefaults<O extends BroadcastDriver> = Record<
2029
O,
2130
Broadcaster[O]["options"]

packages/vue/src/composables/useEcho.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { echo } from "../config";
44
import type {
55
Channel,
66
ChannelData,
7+
ChannelReturnType,
78
Connection,
89
ModelEvents,
910
ModelPayload,
@@ -173,7 +174,7 @@ export const useEcho = <
173174
/**
174175
* Channel instance
175176
*/
176-
channel: () => subscription,
177+
channel: () => subscription as ChannelReturnType<TDriver, TVisibility>,
177178
};
178179
};
179180

packages/vue/src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export type Channel = {
1616
visibility: "private" | "public" | "presence";
1717
};
1818

19+
export type ChannelReturnType<
20+
T extends BroadcastDriver,
21+
V extends Channel["visibility"],
22+
> = V extends "presence"
23+
? Broadcaster[T]["presence"]
24+
: V extends "private"
25+
? Broadcaster[T]["private"]
26+
: Broadcaster[T]["public"];
27+
1928
export type ConfigDefaults<O extends BroadcastDriver> = Record<
2029
O,
2130
Broadcaster[O]["options"]

0 commit comments

Comments
 (0)