Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
¿Cómo puedo extender una clase EventEmitter abstracta, con algunos eventos predeterminados?

¿Alguien podría ayudar con esto, por favor?

¿Cómo puedo extender una clase EventEmitter abstracta, con eventos comunes? (Estoy usando https://github.com/andywer/typed-emitter )

Ejemplo:

 import EventEmitter from "events"; import TypedEmitter from "typed-emitter"; type BaseEvents = { BASE_EVENT: (arg: string) => void; }; abstract class BaseEmitter<T> extends (EventEmitter as { new <T>(): TypedEmitter<T>; })<T> { constructor() { super(); // How can I make this type safe ???? this.emit("BASE_EVENT", "string-value"); // <-- How can I achieve type safety here } } type ImpOneEvents = { C1_EVENT: (a: number) => void; }; class ImpOne extends BaseEmitter<ImpOneEvents> { constructor() { super(); this.emit("C1_EVENT", 1); // OK: type checks ok (GOOD) this.emit("C1_EVENT", "bla"); // ERROR: string not assignable (GOOD) } } type ImpTwoEvents = { C2_EVENT: (a: boolean) => void; }; class ImpTwo extends BaseEmitter<ImpTwoEvents> { constructor() { super(); this.emit("C2_EVENT", true); // OK: type checks ok (GOOD) } } const impTwo = new ImpTwo(); impTwo.on("C2_EVENT", (a) => { parseFloat(a); // ERROR: Type of boolean not assignable to parameter of type string (GOOD) });

He intentado

 type BaseEvents = { BASE_EVENT: (arg: string) => void; }; abstract class BaseEmitter<T = {}> extends (EventEmitter as { new <T>(): TypedEmitter<T>; })<T & BaseEvents> { ...

Y muchas otras cosas Lo anterior da como resultado

Argument of type '[string]' is not assignable to parameter of type 'Arguments<(T & BaseEvents)["BASE_EVENT"]>'.

para linea

 this.emit("BASE_EVENT", "string-value"); // <-- How can I achieve type safety here
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda