I am currently developing an event manager system. The main objective is to allow users to subscribe to events by providing an event type and a callback function. In my implementation, events are represented as classes, where AwesomeEventType in the example below is the class name. The way I envision it is something like this:
eventManager.addEventListener(AwesomeEventType, (event: AwesomeEventType) => doSomething());
In order to achieve this, the addEventListener method is defined as follows:
addEventListener<T>(
eventType: { new (...args: any[]): T},
eventHandler: (event: T) => void
)
Though this setup allows me to subscribe to events, I have noticed that TypeScript does not enforce proper constraints on T. For instance, in the following scenario, I would expect a build error, but none occurs:
eventManager.addEventListener(AwesomeEventType, (event: DifferentEventType) => doSomething())
Is there a way to ensure that TypeScript verifies the callback parameter accepts the correct type and generates an error if it does not?