Generic EventEmitter Class

I am trying to make the eventEmitter class generic, however many type errors occur such as:
Property 'emit' in type 'TypedEventEmitter<T>' is not assignable to the same property in base type 'EventEmitter<string | symbol, any>'.
type EventEmitterTypes<T> = {
[K in keyof T]: (payload: any) => void;
};


class TypedEventEmitter<T> extends EventEmitter {
emit<K extends keyof EventEmitterTypes<T>>(event: K, payload: any): boolean {
return super.emit(event as string, payload);
}

on<K extends keyof EventEmitterTypes<T>>(event: K, listener: EventEmitterTypes<T>[K]): this {
return super.on(event as string, listener);
}

off<K extends keyof EventEmitterTypes<T>>(event: K, listener: EventEmitterTypes<T>[K]): this {
return super.off(event as string, listener);
}

once<K extends keyof EventEmitterTypes<T>>(event: K, listener: EventEmitterTypes<T>[K]): this {
return super.once(event as string, listener);
}
}
type EventEmitterTypes<T> = {
[K in keyof T]: (payload: any) => void;
};


class TypedEventEmitter<T> extends EventEmitter {
emit<K extends keyof EventEmitterTypes<T>>(event: K, payload: any): boolean {
return super.emit(event as string, payload);
}

on<K extends keyof EventEmitterTypes<T>>(event: K, listener: EventEmitterTypes<T>[K]): this {
return super.on(event as string, listener);
}

off<K extends keyof EventEmitterTypes<T>>(event: K, listener: EventEmitterTypes<T>[K]): this {
return super.off(event as string, listener);
}

once<K extends keyof EventEmitterTypes<T>>(event: K, listener: EventEmitterTypes<T>[K]): this {
return super.once(event as string, listener);
}
}
Does anyone know how to fix this? TIA
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server