I'm currently working on a project utilizing the discord.js library. Within this project, there is an interface called ClientEvents
which contains different event argument tuple types:
interface ClientEvents {
ready: [];
warn: [reason: string]
message: [message: Message];
// ...some more events
}
Alongside this, I have created an EventHandler
class that accepts a specific type as an argument:
abstract class EventHandler<E extends keyof ClientEvents> {
protected constructor(public eventName: E) {}
abstract execute(...args: ClientEvents[E]): void;
}
However, I've encountered an issue where I am unable to extend this class using type inference for E
, even when explicitly defined in the super()
call:
// Error: Generic type 'EventHandler<E>' requires 1 type argument(s).
class ReadyHandler extends EventHandler {
public constructor() {
super('ready'); // My intention was to infer the type using this argument
}
public execute() {
// Additional code goes here
}
}
Is there a method to automatically infer class argument types based on the arguments provided in a super()
call?