No, not easily; inference generally doesn't work that way. You could, of course, write it out:
class ReadyHandler extends EventHandler<"ready"> {
public constructor() { super('ready'); }
public execute() {}
}
If you want to avoid that redundancy you could refactor or encapsulate EventHandler
so that you give it an actual string of type E
and E
will be inferred from it. The resulting class will no longer be generic. For example:
function SpecificHandler<E extends keyof ClientEvents>(eventName: E) {
abstract class SpecificHandler extends EventHandler<E> {
constructor() { super(eventName) };
}
return SpecificHandler;
}
class MessageHandler extends SpecificHandler("message") {
public execute(msg: Message) {
}
}
I'm not sure it's worth it, though.
Playground link to code
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…