Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
How can I extend an abstract EventEmitter class, with some default events?

Could anyone help with this please?

How can I extend an abstract EventEmitter class, with common events? (I'm using https://github.com/andywer/typed-emitter)

Example:

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)
});

I've tried

type BaseEvents = {
  BASE_EVENT: (arg: string) => void;
};

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

And lots of other things. The above results in

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

For line

this.emit("BASE_EVENT", "string-value"); // <-- How can I achieve type safety here
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda