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

227
Visualizações
Socket.io v4 Event Type Errors When Wrapping in a Function

I'm trying to implement event typeguards, as they're available in Socket.io v4.

This is my attempt of creating an onAndOff function, that listens for a given socket event, and returns a cleanup callback (will later be used in a useEffect):

// somewhere in another file:
export const socket: Socket<ServerToClient, ClientToServer> = io();


function onAndOff<
    Event extends keyof ServerToClient = keyof ServerToClient,
    Callback = ServerToClient[Event]
>(event: Event, callback: Callback) {
    socket.on(event, callback);
    return () => socket.off(event, callback);
}

But my IDE marks the callback arguments as an error:

TS2345: Argument of type 'Callback' is not assignable to parameter of type 'FallbackToUntypedListener '.

The error message shown

It seems to be a generic Typescript error, but I'm not sure what wrong with this wrapper function.

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