mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
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:
1 parent
f23e442c4c
commit
fc6e9d0002
2 files changed
+6
-6
No files matched your search
@@ -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[];
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user