From 4e22b8d5359b3bedd8cb83e4d61292e0f29fd409 Mon Sep 17 00:00:00 2001 From: Gregory Beaver Date: Sun, 1 Sep 2019 12:41:26 -0400 Subject: [PATCH 1/2] add overloads to compose.ts --- src/compose.ts | 91 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 6 deletions(-) diff --git a/src/compose.ts b/src/compose.ts index fed1b7860b..2ed13051a2 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -1,13 +1,92 @@ +type Func0 = () => R +type Func1 = (a1: T1) => R +type Func2 = (a1: T1, a2: T2) => R +type Func3 = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R + /** * Composes single-argument functions from right to left. The rightmost - * function can take multiple arguments as it provides the signature for - * the resulting composite function. + * function can take multiple arguments as it provides the signature for the + * resulting composite function. * - * @param {...Function} funcs The functions to compose. - * @returns {Function} A function obtained by composing the argument functions - * from right to left. For example, compose(f, g, h) is identical to doing - * (...args) => f(g(h(...args))). + * @param funcs The functions to compose. + * @returns R function obtained by composing the argument functions from right + * to left. For example, `compose(f, g, h)` is identical to doing + * `(...args) => f(g(h(...args)))`. */ +export default function compose(): (a: R) => R + +export default function compose(f: F): F + +/* two functions */ +export default function compose(f1: (b: A) => R, f2: Func0): Func0 +export default function compose( + f1: (b: A) => R, + f2: Func1 +): Func1 +export default function compose( + f1: (b: A) => R, + f2: Func2 +): Func2 +export default function compose( + f1: (b: A) => R, + f2: Func3 +): Func3 + +/* three functions */ +export default function compose( + f1: (b: B) => R, + f2: (a: A) => B, + f3: Func0 +): Func0 +export default function compose( + f1: (b: B) => R, + f2: (a: A) => B, + f3: Func1 +): Func1 +export default function compose( + f1: (b: B) => R, + f2: (a: A) => B, + f3: Func2 +): Func2 +export default function compose( + f1: (b: B) => R, + f2: (a: A) => B, + f3: Func3 +): Func3 + +/* four functions */ +export default function compose( + f1: (b: C) => R, + f2: (a: B) => C, + f3: (a: A) => B, + f4: Func0 +): Func0 +export default function compose( + f1: (b: C) => R, + f2: (a: B) => C, + f3: (a: A) => B, + f4: Func1 +): Func1 +export default function compose( + f1: (b: C) => R, + f2: (a: B) => C, + f3: (a: A) => B, + f4: Func2 +): Func2 +export default function compose( + f1: (b: C) => R, + f2: (a: B) => C, + f3: (a: A) => B, + f4: Func3 +): Func3 + +/* rest */ +export default function compose( + f1: (b: any) => R, + ...funcs: Function[] +): (...args: any[]) => R + +export default function compose(...funcs: Function[]): (...args: any[]) => R export default function compose(...funcs: Function[]) { if (funcs.length === 0) { From 9f21284339987305b86c5774184eba798d299e48 Mon Sep 17 00:00:00 2001 From: Gregory Beaver Date: Mon, 2 Sep 2019 10:45:31 -0400 Subject: [PATCH 2/2] add applyMiddleware overload fixes --- src/applyMiddleware.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/applyMiddleware.ts b/src/applyMiddleware.ts index e7c769bd42..fe8edefa11 100644 --- a/src/applyMiddleware.ts +++ b/src/applyMiddleware.ts @@ -20,6 +20,35 @@ import { Reducer } from './types/reducers' * @param {...Function} middlewares The middleware chain to be applied. * @returns {Function} A store enhancer applying the middleware. */ +export default function applyMiddleware(): StoreEnhancer +export default function applyMiddleware( + middleware1: Middleware +): StoreEnhancer<{ dispatch: Ext1 }> +export default function applyMiddleware( + middleware1: Middleware, + middleware2: Middleware +): StoreEnhancer<{ dispatch: Ext1 & Ext2 }> +export default function applyMiddleware( + middleware1: Middleware, + middleware2: Middleware, + middleware3: Middleware +): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 }> +export default function applyMiddleware( + middleware1: Middleware, + middleware2: Middleware, + middleware3: Middleware, + middleware4: Middleware +): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }> +export default function applyMiddleware( + middleware1: Middleware, + middleware2: Middleware, + middleware3: Middleware, + middleware4: Middleware, + middleware5: Middleware +): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }> +export default function applyMiddleware( + ...middlewares: Middleware[] +): StoreEnhancer<{ dispatch: Ext }> export default function applyMiddleware( ...middlewares: Middleware[] ): StoreEnhancer { @@ -40,7 +69,7 @@ export default function applyMiddleware( dispatch: (action, ...args) => dispatch(action, ...args) } const chain = middlewares.map(middleware => middleware(middlewareAPI)) - dispatch = compose(...chain)(store.dispatch) + dispatch = compose(...chain)(store.dispatch) return { ...store,