Skip to content

Emitting an event with acknowledgement and timeout breaks the Typescript client-to-server event definitions #1555

@ltomov

Description

@ltomov

Describe the bug
Emitting an event using a timeout and acknowledgement causes the acknowledgement function to change its arguments list, so that Error is first argument. This breaks the Typescript definitions. See below for more details:

To Reproduce

Socket.IO client version: 4.5.2

Client

import { io } from "socket.io-client";

interface ClientToServerEvents {
    "withAck": (callback: (data: any) => void) => void;
}
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io("ws://localhost:3000/", {});

socket.on("connect", () => {
  socket.timeout(5000).emit("withAck", (error, data) => {    // typescript throws here, since the function takes just one parameter as per the ClientToServerEvents interface
        /* If .timeout() is not used, then the first argument is data.
            If .timeout() is used, then first argument is error, second one is data.
            This breaks the ClientToServerEvents interface.
            If you change the ClientToServerEvents interface to have an error argument, 
            then calling the callback from the server breaks, since there data is always first parameter. 
            You need to use different interfaces for client and server.
       */
   });
});

Expected behavior
It should be possible to define one ClientToServerEvents interface and use it both with client and server, irrespective if events are emitted with timeout or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions