Fix type checking on Request and Event types

When defining a generic type where a type argument is never used
as a part of the type itself, TypeScript will consider two instances of
the generic type with different values for the type to be the same.
This caused the handle method to accept any handler for any
Request/Event, ignoring the event/request type completely.

Fix by specifying the constructor to return the intersection of the type
itself and the Request/Event type.
This commit is contained in:
Hornwitser committed 2023-11-24 20:56:02 +01:00
1 parent f23e442c4c
commit fc6e9d0002
2 files changed
+6 -6

No files matched your search

+4 -4
View File
@@ -11,8 +11,8 @@ import type { User } from "../users";
import type { AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent } from "../data";
export interface Request<Req, Res> {
constructor: Partial<JSONDeserialisable<Request<Req, Res>>> & {
new (...args: any): Request<Req, Res>,
constructor: Partial<JSONDeserialisable<Req & Request<Req, Res>>> & {
new (...args: any): Req & Request<Req, Res>,
name: string,
type: "request";
src: AddressType | readonly AddressType[];
@@ -25,8 +25,8 @@ export interface Request<Req, Res> {
export type RequestClass<Req, Res> = Request<Req, Res>["constructor"];
export interface Event<T> {
constructor: Partial<JSONDeserialisable<Event<T>>> & {
new (...args: any): Event<T>,
constructor: Partial<JSONDeserialisable<T & Event<T>>> & {
new (...args: any): T & Event<T>,
name: string,
type: "event";
src: AddressType | readonly AddressType[];
+2 -2
View File
@@ -215,7 +215,7 @@ export class SubscriptionController {
* Channels are an optional feature which can be implemented to only get notifications of reliant updates
* Multiple handlers can be subscribed at the same time, on the same or different channels
*/
export class EventSubscriber<T, V=T> {
export class EventSubscriber<T extends Event<T>, V=T> {
_callbacks = new Array<EventSubscriberCallback<V>>();
_channelCallbacks = new Map<string | number, Array<EventSubscriberCallback<V>>>();
lastResponse: Event<T> | null = null;
@@ -365,7 +365,7 @@ export class EventSubscriber<T, V=T> {
));
if (response.eventReplay) {
await this._handle(response.eventReplay);
await this._handle(response.eventReplay as any);
}
}
}