Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/socket.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export type AllButLast<T extends any[]> = T extends [...infer H, infer L]
export type FirstArg<T> = T extends (arg: infer Param) => infer Result
? Param
: any;
export type FirstNonErrorTuple<T extends unknown[]> = T[0] extends Error
? T[1]
: T[0];
/**
* The type of the value of the acknowledgement: the first argument of the callback, or the second one when the
* timeout() flag adds an error as first argument.
*/
export type FirstNonErrorArg<T> = T extends (...args: infer Params) => any
? FirstNonErrorTuple<Params>
: any;

export interface SocketOptions {
/**
Expand Down Expand Up @@ -514,7 +524,7 @@ export class Socket<
public emitWithAck<Ev extends EventNames<EmitEvents>>(
ev: Ev,
...args: AllButLast<EventParams<EmitEvents, Ev>>
): Promise<FirstArg<Last<EventParams<EmitEvents, Ev>>>> {
): Promise<FirstNonErrorArg<Last<EventParams<EmitEvents, Ev>>>> {
return new Promise((resolve, reject) => {
const fn = (arg1, arg2) => {
return arg1 ? reject(arg1) : resolve(arg2);
Expand Down