stream-chat-js
stream-chat-js copied to clipboard
Feature Request: Improved EventHandler typings
Currently the Event type is this:
export type Event<
AttachmentType extends UnknownType = UnknownType,
ChannelType extends UnknownType = UnknownType,
CommandType extends string = LiteralStringForUnion,
EventType extends UnknownType = UnknownType,
MessageType extends UnknownType = UnknownType,
ReactionType extends UnknownType = UnknownType,
UserType extends UnknownType = UnknownType
> = EventType & {
type: EventTypes;
channel?: ChannelResponse<ChannelType, CommandType, UserType>;
channel_id?: string;
channel_type?: string;
cid?: string;
clear_history?: boolean;
connection_id?: string;
created_at?: string;
hard_delete?: boolean;
mark_messages_deleted?: boolean;
me?: OwnUserResponse<ChannelType, CommandType, UserType>;
member?: ChannelMemberResponse<UserType>;
message?: MessageResponse<
AttachmentType,
ChannelType,
CommandType,
MessageType,
ReactionType,
UserType
>;
online?: boolean;
parent_id?: string;
reaction?: ReactionResponse<ReactionType, UserType>;
received_at?: string | Date;
total_unread_count?: number;
unread_channels?: number;
unread_count?: number;
user?: UserResponse<UserType>;
user_id?: string;
watcher_count?: number;
};
Every single property is optional. While I understand this is the simplest way to implement this type, it doesn't really help developers all that much.
The problem is every different event has different event properties and this makes it cumbersome to develop features without triggering events and reverse-engineering what the actual event body looks like and working from there.
This documentation doesn't help either: https://getstream.io/chat/docs/react/event_object/?language=javascript
Since you all are the source of truth on this, perhaps we could get an update that has a properly typed Event for every different event.type?
I imagine a lot of users of this library have something like the following:
import { EventHandler, StreamChat } from "stream-chat";
const eventListener: EventHandler = (event) => {
switch (event.type) {
case "channel.created":
// have event attributes properly show exactly what would be on a channel.created event
break;
case "channel.deleted":
// have event attributes properly show exactly what would be on a channel.deleted event
break;
}
}
const client = StreamChat.getInstance(process.env.STREAM_KEY)
client.on(eventListener);
If we had proper typings per event.type, it'd increase confidence in what is returned and would make development a lot easier.
gz#12041
Agent comment from Vishal Narkhede in Zendesk ticket #12041:
Hey,
This task is already in progress. We should be able to publish it by next week approximately. Please keep an eye for following PR and release after this.
https://github.com/GetStream/stream-chat-js/pull/672
I am going to close this ticket for now. Please reopen if you have any other questions!!
Thanks,
Vishal
°°°
Hello, we're still struggling with typing custom events, is there any way it can be improved in the following releases?
I've seen a PR which was closed in 2021 because it was going to introduce a breaking change but preventing the library from getting better in terms of DX seems quite counter intuitive for me.
@tplk thanks for reaching out, I'll be closing this specific issue. Can you please open a new one with sufficient information about the specific customer events typings you've mentioned?