socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Typescript type issues

Open turnerhayes opened this issue 5 months ago • 2 comments

Describe the bug

Typescript types are not working correctly, specifically for event listeners. on() and related methods don't seem to accept the callbacks.

For example, I have a client class that I want to expose passthrough methods for event listeners. I defined the passthrough method as


    listen<E extends keyof ServerToClientEvents>(eventName: E, callback: ServerToClientEvents[E]) {
        this.socket.on(eventName, callback);
    }

It yields this error:

TS2345: Argument of type 'ServerToClientEvents[E]' is not assignable to parameter of type 'FallbackToUntypedListener<E extends "connect" | "disconnect" | "connect_error" ? SocketReservedEvents[E] : E extends "server-to-client-1" ? ServerToClientEvents[E] : never>'.
  Type '(arg: string[]) => void' is not assignable to type 'FallbackToUntypedListener<E extends "connect" | "disconnect" | "connect_error" ? SocketReservedEvents[E] : E extends "server-to-client-1" ? ServerToClientEvents[E] : never>'.

#5367 is also demonstrated in this repo.

To Reproduce

Reproduction repo: https://github.com/turnerhayes/socket-io-type-confusion

turnerhayes avatar Jul 02 '25 03:07 turnerhayes