Skip to content

Commit

Permalink
fix(typings): properly type emits with timeout (2)
Browse files Browse the repository at this point in the history
This follows [1], in order to keep the label of each argument.

[1]: 33e4172

Related:

- #1570 (comment)
- microsoft/TypeScript#39941
- microsoft/TypeScript#48049
  • Loading branch information
darrachequesne committed Jan 17, 2023
1 parent 33e4172 commit a1c528b
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import debugModule from "debug"; // debug()

const debug = debugModule("socket.io-client:socket"); // debug()

type Last<T extends any[]> = T extends [...infer I, infer L] ? L : any;
type AllButLast<T extends any[]> = T extends [...infer I, infer L] ? I : any[];

type PrependTimeoutError<T> = T extends (...args: infer Params) => infer Result
? (err: Error, ...args: Params) => Result
: T;
type PrependTimeoutError<T extends any[]> = {
[K in keyof T]: T[K] extends (...args: infer Params) => infer Result
? (err: Error, ...args: Params) => Result
: T[K];
};

/**
* Utility type to decorate the acknowledgement callbacks with a timeout error.
Expand All @@ -40,9 +39,7 @@ type PrependTimeoutError<T> = T extends (...args: infer Params) => infer Result
*/
export type DecorateAcknowledgements<E> = {
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result
? (
...args: [...AllButLast<Params>, PrependTimeoutError<Last<Params>>]
) => Result
? (...args: PrependTimeoutError<Params>) => Result
: E[K];
};

Expand Down

0 comments on commit a1c528b

Please sign in to comment.