UNPKG

3 kBTypeScriptView Raw
1/**
2 * The type of a function that can be converted to FP.
3 */
4export type FPFnInput = (...args: any[]) => any;
5/**
6 * The supported arity type.
7 */
8export type FPArity = 1 | 2 | 3 | 4;
9/**
10 * FP function interface. It infers the arity of the function and returns the
11 * corresponding FP function interface.
12 */
13export type FPFn<Fn extends FPFnInput, Arity extends FPArity> = Arity extends 4
14 ? FPFn4<
15 ReturnType<Fn>,
16 Parameters<Fn>[3],
17 Parameters<Fn>[2],
18 Parameters<Fn>[1],
19 Parameters<Fn>[0]
20 >
21 : Arity extends 3
22 ? FPFn3<
23 ReturnType<Fn>,
24 Parameters<Fn>[2],
25 Parameters<Fn>[1],
26 Parameters<Fn>[0]
27 >
28 : Arity extends 2
29 ? FPFn2<ReturnType<Fn>, Parameters<Fn>[1], Parameters<Fn>[0]>
30 : Arity extends 1
31 ? FPFn1<ReturnType<Fn>, Parameters<Fn>[0]>
32 : never;
33/**
34 * FP function interface with 1 arguments.
35 */
36export interface FPFn1<Result, Arg> {
37 /**
38 * Curried version of the function. Returns itself.
39 */
40 (): FPFn1<Result, Arg>;
41 /**
42 * Returns the result of the function call.
43 */
44 (arg: Arg): Result;
45}
46/**
47 * FP function interface with 2 arguments.
48 */
49export interface FPFn2<Result, Arg2, Arg1> {
50 /**
51 * Curried version of the function. Returns itself.
52 */
53 (): FPFn2<Result, Arg2, Arg1>;
54 /**
55 * Curried version of the function. Returns a function that accepts the rest
56 * arguments.
57 */
58 (arg2: Arg2): FPFn1<Result, Arg1>;
59 /**
60 * Returns the result of the function call.
61 */
62 (arg2: Arg2, arg1: Arg1): Result;
63}
64/**
65 * FP function interface with 3 arguments.
66 */
67export interface FPFn3<Result, Arg3, Arg2, Arg1> {
68 /**
69 * Curried version of the function. Returns itself.
70 */
71 (): FPFn3<Result, Arg3, Arg2, Arg1>;
72 /**
73 * Curried version of the function. Returns a function that accepts the rest
74 * arguments.
75 */
76 (arg3: Arg3): FPFn2<Result, Arg2, Arg1>;
77 /**
78 * Curried version of the function. Returns a function that accepts the rest
79 * arguments.
80 */
81 (arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
82 /**
83 * Returns the result of the function call.
84 */
85 (arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
86}
87/**
88 * FP function interface with 4 arguments.
89 */
90export interface FPFn4<Result, Arg4, Arg3, Arg2, Arg1> {
91 /**
92 * Curried version of the function. Returns itself.
93 */
94 (): FPFn4<Result, Arg4, Arg3, Arg2, Arg1>;
95 /**
96 * Curried version of the function. Returns a function that accepts the rest
97 * arguments.
98 */
99 (arg4: Arg4): FPFn3<Result, Arg3, Arg2, Arg1>;
100 /**
101 * Curried version of the function. Returns a function that accepts the rest
102 * arguments.
103 */
104 (arg4: Arg4, arg3: Arg3): FPFn2<Result, Arg2, Arg1>;
105 /**
106 * Curried version of the function. Returns a function that accepts the rest
107 * arguments.
108 */
109 (arg4: Arg4, arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
110 /**
111 * Returns the result of the function call.
112 */
113 (arg4: Arg4, arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
114}