Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!