From fc6e9d000259235fed25eeacecdce0e6715a0ba7 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 24 Nov 2023 19:05:30 +0100 Subject: [PATCH] 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. --- packages/lib/src/link/link.ts | 8 ++++---- packages/lib/src/subscriptions.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/lib/src/link/link.ts b/packages/lib/src/link/link.ts index 998e3a63..ebc5cecf 100644 --- a/packages/lib/src/link/link.ts +++ b/packages/lib/src/link/link.ts @@ -11,8 +11,8 @@ import type { User } from "../users"; import type { AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent } from "../data"; export interface Request { - constructor: Partial>> & { - new (...args: any): Request, + constructor: Partial>> & { + new (...args: any): Req & Request, name: string, type: "request"; src: AddressType | readonly AddressType[]; @@ -25,8 +25,8 @@ export interface Request { export type RequestClass = Request["constructor"]; export interface Event { - constructor: Partial>> & { - new (...args: any): Event, + constructor: Partial>> & { + new (...args: any): T & Event, name: string, type: "event"; src: AddressType | readonly AddressType[]; diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index eb82e02e..ab7b4c7f 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -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 { +export class EventSubscriber, V=T> { _callbacks = new Array>(); _channelCallbacks = new Map>>(); lastResponse: Event | null = null; @@ -365,7 +365,7 @@ export class EventSubscriber { )); if (response.eventReplay) { - await this._handle(response.eventReplay); + await this._handle(response.eventReplay as any); } } }