Skip to content

Commit

Permalink
feat: use varargs types instead of overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
andnp committed Mar 11, 2019
1 parent 9d2e009 commit a2c1162
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
20 changes: 2 additions & 18 deletions src/types/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,12 @@ export type AnyFunc<R = any> = (...args: any[]) => R;
* @returns the function `F` with new return value `R`
*/
export type OverwriteReturn<F extends AnyFunc, R> =
F extends () => any ? () => R :
F extends (x1: infer X1) => any ? (x1: X1) => R :
F extends (x1: infer X1, x2: infer X2) => any ? (x1: X1, x2: X2) => R :
F extends (x1: infer X1, x2: infer X2, x3: infer X3) => any ? (x1: X1, x2: X2, x3: X3) => R :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4) => any ? (x1: X1, x2: X2, x3: X3, x4: X4) => R :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5) => any ? (x1: X1, x2: X2, x3: X3, x4: X4, x5: X5) => R :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6) => any ? (x1: X1, x2: X2, x3: X3, x4: X4, x5: X5, x6: X6) => R :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6, x7: infer X7) => any ? (x1: X1, x2: X2, x3: X3, x4: X4, x5: X5, x6: X6, x7: X7) => R :
AnyFunc<R>;
F extends ((...x: infer T) => unknown) ? ((...x: T) => R) : AnyFunc<R>;

/**
* Returns a tuple type of a functions arguments up to 7.
* @param F a function with up to 7 arguments
* @returns a tuple containing `F`'s argument types
*/
export type ArgsAsTuple<F extends AnyFunc> =
F extends () => any ? void[] :
F extends (x1: infer X1) => any ? [X1] :
F extends (x1: infer X1, x2: infer X2) => any ? [X1, X2] :
F extends (x1: infer X1, x2: infer X2, x3: infer X3) => any ? [X1, X2, X3] :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4) => any ? [X1, X2, X3, X4] :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5) => any ? [X1, X2, X3, X4, X5] :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6) => any ? [X1, X2, X3, X4, X5, X6] :
F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6, x7: infer X7) => any ? [X1, X2, X3, X4, X5, X6, X7] :
any[];
F extends ((...x: infer T) => unknown) ? T : any[];
2 changes: 1 addition & 1 deletion test/functions/ArgsAsTuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test("Can get a tuple of function's argument types", t => {
type F2 = (x: number, y: string) => any;
type F3 = (x: number, y: string, z: boolean) => any;

type E0 = void[];
type E0 = [];
type E1 = [number];
type E2 = [number, string];
type E3 = [number, string, boolean];
Expand Down

0 comments on commit a2c1162

Please sign in to comment.