Skip to content

fix(sio-client/types): fix the return type of emitWithAck() with the timeout() flag - #5627

Open
mark1russell7 wants to merge 1 commit into
socketio:mainfrom
mark1russell7:fix/sio-client-timeout-emitwithack-type
Open

mark1russell7 wants to merge 1 commit into
socketio:mainfrom
mark1russell7:fix/sio-client-timeout-emitwithack-type

Conversation

@mark1russell7

Copy link
Copy Markdown

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

With the timeout() flag, emitWithAck() returns Promise<any>. FirstArg only matches a callback with one argument, and timeout() adds an error as first argument of the callback.

#4925 reported this. The server side was fixed in #4817 with FirstNonErrorArg, and in #4925 the maintainer said that the same change is needed on the client side.

New behavior

The client uses the same FirstNonErrorTuple and FirstNonErrorArg types as the server (packages/socket.io/lib/typed-events.ts). FirstArg stays exported.

test/typed-events.test-d.ts already expects string for this value, but the script test:types is exit 0 since 8bead0f (tsd is not upgraded for TypeScript 6), so the type tests do not run. So the check below ran with tsc (TypeScript 6.0.3, strict, module: nodenext) on a file in test/, against the build of the package:

// F0062: the type of the value of socket.timeout(ms).emitWithAck(). The events are those of test/typed-events.test-d.ts.
// The check fails when the type is any. The hooks copy this file into packages/socket.io-client/test/.
import { io, Socket } from "../build/cjs/index.js";

interface ClientToServerEvents {
  ackFromClientSingleArg: (a: string, b: number, ack: (c: string) => void) => void;
}

type IsAny<T> = 0 extends 1 & T ? true : false;

export async function check() {
  const socket: Socket<{}, ClientToServerEvents> = io();

  const value = await socket.emitWithAck("ackFromClientSingleArg", "1", 2);
  const valueIsAny: IsAny<typeof value> = false;

  const valueWithTimeout = await socket.timeout(1000).emitWithAck("ackFromClientSingleArg", "3", 4);
  const valueWithTimeoutIsAny: IsAny<typeof valueWithTimeout> = false;
  const valueWithTimeoutIsString: string = valueWithTimeout;

  return [valueIsAny, valueWithTimeoutIsAny, valueWithTimeoutIsString];
}

The check ran in GitHub Actions (Node.js 24.22.2 on Linux): the check with the lib folder of upstream main (before), then with this change (after), then npm test of socket.io-client with this change:

upstream main: f97d51a810d4f266122eb99bb6c083cd383638f0
== F0062 (fix/sio-client-timeout-emitwithack-type)
  commit d496ed45, base behind
  F0062 tsc (before):
    Version 6.0.3
    test/check-F0062.ts(18,9): error TS2322: Type 'false' is not assignable to type 'true'.
    exit=2
  F0062 tsc (after):
    Version 6.0.3
    exit=0
  npm test:  115 passing (7s)  exit=0

Other information (e.g. related issues)

Related: #4925, #4817

This PR was prepared with AI assistance (Claude). The tests above were run before and after the change, with the results above.

🤖 Generated with Claude Code

…timeout() flag

FirstArg only matches a callback with one argument. With the timeout()
flag, the callback gets an error as first argument, so the type of the
value was any instead of the type of the acknowledgement.

This uses the same approach as FirstNonErrorArg in the socket.io package.
The type test in test/typed-events.test-d.ts already expects this type,
but the test:types script is "exit 0" since 8bead0f, so it does not run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant