I have been working on creating a versatile base event class:
class BaseEvent<T extends { (args?: any[]): void }> {
addEventListener(listener: T): { (): void } {
return () => { };
}
}
I want to extend this base class to define specific callback parameters:
class ExtraSpecialEvent
extends BaseEvent<{ (foo: string): void }> {
}
However, I am struggling with the syntax. Here is a playground showcasing my issue.
Does anyone have any suggestions on how to achieve this?
---- UPDATE ----
Thanks to @murat-k's response below, I realized that my generic was expecting an array... However, this is not what I intended. I actually wanted to allow for 0 or more arguments of type any
. The solution to my problem was to update the generic to:
class BaseEvent<T extends { (...args: any[]): void }> {
addEventListener(listener: T): { (): void } {
return () => {};
}
}