Skip to content

Commit 3774004

Browse files
cellogtimdorr
authored andcommitted
add overloads to compose.ts and applyMiddleware.ts (reduxjs#3558)
* add overloads to compose.ts * add applyMiddleware overload fixes Former-commit-id: 6c609ac
1 parent 320cd54 commit 3774004

File tree

2 files changed

+115
-7
lines changed

2 files changed

+115
-7
lines changed

src/applyMiddleware.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ import { Reducer } from './types/reducers'
2020
* @param {...Function} middlewares The middleware chain to be applied.
2121
* @returns {Function} A store enhancer applying the middleware.
2222
*/
23+
export default function applyMiddleware(): StoreEnhancer
24+
export default function applyMiddleware<Ext1, S>(
25+
middleware1: Middleware<Ext1, S, any>
26+
): StoreEnhancer<{ dispatch: Ext1 }>
27+
export default function applyMiddleware<Ext1, Ext2, S>(
28+
middleware1: Middleware<Ext1, S, any>,
29+
middleware2: Middleware<Ext2, S, any>
30+
): StoreEnhancer<{ dispatch: Ext1 & Ext2 }>
31+
export default function applyMiddleware<Ext1, Ext2, Ext3, S>(
32+
middleware1: Middleware<Ext1, S, any>,
33+
middleware2: Middleware<Ext2, S, any>,
34+
middleware3: Middleware<Ext3, S, any>
35+
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 }>
36+
export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>(
37+
middleware1: Middleware<Ext1, S, any>,
38+
middleware2: Middleware<Ext2, S, any>,
39+
middleware3: Middleware<Ext3, S, any>,
40+
middleware4: Middleware<Ext4, S, any>
41+
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }>
42+
export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(
43+
middleware1: Middleware<Ext1, S, any>,
44+
middleware2: Middleware<Ext2, S, any>,
45+
middleware3: Middleware<Ext3, S, any>,
46+
middleware4: Middleware<Ext4, S, any>,
47+
middleware5: Middleware<Ext5, S, any>
48+
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }>
49+
export default function applyMiddleware<Ext, S = any>(
50+
...middlewares: Middleware<any, S, any>[]
51+
): StoreEnhancer<{ dispatch: Ext }>
2352
export default function applyMiddleware(
2453
...middlewares: Middleware[]
2554
): StoreEnhancer {
@@ -40,7 +69,7 @@ export default function applyMiddleware(
4069
dispatch: (action, ...args) => dispatch(action, ...args)
4170
}
4271
const chain = middlewares.map(middleware => middleware(middlewareAPI))
43-
dispatch = compose(...chain)(store.dispatch)
72+
dispatch = compose<typeof dispatch>(...chain)(store.dispatch)
4473

4574
return {
4675
...store,

src/compose.ts

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,92 @@
1+
type Func0<R> = () => R
2+
type Func1<T1, R> = (a1: T1) => R
3+
type Func2<T1, T2, R> = (a1: T1, a2: T2) => R
4+
type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R
5+
16
/**
27
* Composes single-argument functions from right to left. The rightmost
3-
* function can take multiple arguments as it provides the signature for
4-
* the resulting composite function.
8+
* function can take multiple arguments as it provides the signature for the
9+
* resulting composite function.
510
*
6-
* @param {...Function} funcs The functions to compose.
7-
* @returns {Function} A function obtained by composing the argument functions
8-
* from right to left. For example, compose(f, g, h) is identical to doing
9-
* (...args) => f(g(h(...args))).
11+
* @param funcs The functions to compose.
12+
* @returns R function obtained by composing the argument functions from right
13+
* to left. For example, `compose(f, g, h)` is identical to doing
14+
* `(...args) => f(g(h(...args)))`.
1015
*/
16+
export default function compose(): <R>(a: R) => R
17+
18+
export default function compose<F extends Function>(f: F): F
19+
20+
/* two functions */
21+
export default function compose<A, R>(f1: (b: A) => R, f2: Func0<A>): Func0<R>
22+
export default function compose<A, T1, R>(
23+
f1: (b: A) => R,
24+
f2: Func1<T1, A>
25+
): Func1<T1, R>
26+
export default function compose<A, T1, T2, R>(
27+
f1: (b: A) => R,
28+
f2: Func2<T1, T2, A>
29+
): Func2<T1, T2, R>
30+
export default function compose<A, T1, T2, T3, R>(
31+
f1: (b: A) => R,
32+
f2: Func3<T1, T2, T3, A>
33+
): Func3<T1, T2, T3, R>
34+
35+
/* three functions */
36+
export default function compose<A, B, R>(
37+
f1: (b: B) => R,
38+
f2: (a: A) => B,
39+
f3: Func0<A>
40+
): Func0<R>
41+
export default function compose<A, B, T1, R>(
42+
f1: (b: B) => R,
43+
f2: (a: A) => B,
44+
f3: Func1<T1, A>
45+
): Func1<T1, R>
46+
export default function compose<A, B, T1, T2, R>(
47+
f1: (b: B) => R,
48+
f2: (a: A) => B,
49+
f3: Func2<T1, T2, A>
50+
): Func2<T1, T2, R>
51+
export default function compose<A, B, T1, T2, T3, R>(
52+
f1: (b: B) => R,
53+
f2: (a: A) => B,
54+
f3: Func3<T1, T2, T3, A>
55+
): Func3<T1, T2, T3, R>
56+
57+
/* four functions */
58+
export default function compose<A, B, C, R>(
59+
f1: (b: C) => R,
60+
f2: (a: B) => C,
61+
f3: (a: A) => B,
62+
f4: Func0<A>
63+
): Func0<R>
64+
export default function compose<A, B, C, T1, R>(
65+
f1: (b: C) => R,
66+
f2: (a: B) => C,
67+
f3: (a: A) => B,
68+
f4: Func1<T1, A>
69+
): Func1<T1, R>
70+
export default function compose<A, B, C, T1, T2, R>(
71+
f1: (b: C) => R,
72+
f2: (a: B) => C,
73+
f3: (a: A) => B,
74+
f4: Func2<T1, T2, A>
75+
): Func2<T1, T2, R>
76+
export default function compose<A, B, C, T1, T2, T3, R>(
77+
f1: (b: C) => R,
78+
f2: (a: B) => C,
79+
f3: (a: A) => B,
80+
f4: Func3<T1, T2, T3, A>
81+
): Func3<T1, T2, T3, R>
82+
83+
/* rest */
84+
export default function compose<R>(
85+
f1: (b: any) => R,
86+
...funcs: Function[]
87+
): (...args: any[]) => R
88+
89+
export default function compose<R>(...funcs: Function[]): (...args: any[]) => R
1190

1291
export default function compose(...funcs: Function[]) {
1392
if (funcs.length === 0) {

0 commit comments

Comments
 (0)