-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
use ts-toolbelt for 'compose' types #3563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ export default function applyMiddleware( | |
dispatch: (action, ...args) => dispatch(action, ...args) | ||
} | ||
const chain = middlewares.map(middleware => middleware(middlewareAPI)) | ||
dispatch = compose<typeof dispatch>(...chain)(store.dispatch) | ||
dispatch = compose(...chain)(store.dispatch as typeof dispatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The next step is to get the chain properly typed so we don't need to do this casting. |
||
|
||
return { | ||
...store, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
type Func0<R> = () => R | ||
type Func1<T1, R> = (a1: T1) => R | ||
type Func2<T1, T2, R> = (a1: T1, a2: T2) => R | ||
type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R | ||
import { F, T } from 'ts-toolbelt' | ||
|
||
/** | ||
* Composes single-argument functions from right to left. The rightmost | ||
|
@@ -13,85 +10,15 @@ type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R | |
* to left. For example, `compose(f, g, h)` is identical to doing | ||
* `(...args) => f(g(h(...args)))`. | ||
*/ | ||
export default function compose(): <R>(a: R) => R | ||
|
||
export default function compose<F extends Function>(f: F): F | ||
|
||
/* two functions */ | ||
export default function compose<A, R>(f1: (b: A) => R, f2: Func0<A>): Func0<R> | ||
export default function compose<A, T1, R>( | ||
f1: (b: A) => R, | ||
f2: Func1<T1, A> | ||
): Func1<T1, R> | ||
export default function compose<A, T1, T2, R>( | ||
f1: (b: A) => R, | ||
f2: Func2<T1, T2, A> | ||
): Func2<T1, T2, R> | ||
export default function compose<A, T1, T2, T3, R>( | ||
f1: (b: A) => R, | ||
f2: Func3<T1, T2, T3, A> | ||
): Func3<T1, T2, T3, R> | ||
|
||
/* three functions */ | ||
export default function compose<A, B, R>( | ||
f1: (b: B) => R, | ||
f2: (a: A) => B, | ||
f3: Func0<A> | ||
): Func0<R> | ||
export default function compose<A, B, T1, R>( | ||
f1: (b: B) => R, | ||
f2: (a: A) => B, | ||
f3: Func1<T1, A> | ||
): Func1<T1, R> | ||
export default function compose<A, B, T1, T2, R>( | ||
f1: (b: B) => R, | ||
f2: (a: A) => B, | ||
f3: Func2<T1, T2, A> | ||
): Func2<T1, T2, R> | ||
export default function compose<A, B, T1, T2, T3, R>( | ||
f1: (b: B) => R, | ||
f2: (a: A) => B, | ||
f3: Func3<T1, T2, T3, A> | ||
): Func3<T1, T2, T3, R> | ||
|
||
/* four functions */ | ||
export default function compose<A, B, C, R>( | ||
f1: (b: C) => R, | ||
f2: (a: B) => C, | ||
f3: (a: A) => B, | ||
f4: Func0<A> | ||
): Func0<R> | ||
export default function compose<A, B, C, T1, R>( | ||
f1: (b: C) => R, | ||
f2: (a: B) => C, | ||
f3: (a: A) => B, | ||
f4: Func1<T1, A> | ||
): Func1<T1, R> | ||
export default function compose<A, B, C, T1, T2, R>( | ||
f1: (b: C) => R, | ||
f2: (a: B) => C, | ||
f3: (a: A) => B, | ||
f4: Func2<T1, T2, A> | ||
): Func2<T1, T2, R> | ||
export default function compose<A, B, C, T1, T2, T3, R>( | ||
f1: (b: C) => R, | ||
f2: (a: B) => C, | ||
f3: (a: A) => B, | ||
f4: Func3<T1, T2, T3, A> | ||
): Func3<T1, T2, T3, R> | ||
|
||
/* rest */ | ||
export default function compose<R>( | ||
f1: (b: any) => R, | ||
...funcs: Function[] | ||
): (...args: any[]) => R | ||
|
||
export default function compose<R>(...funcs: Function[]): (...args: any[]) => R | ||
|
||
export default function compose(...funcs: Function[]) { | ||
const compose = <Fns extends F.Function<any[], any>[]>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really wanted to use ts-toolbelt's |
||
...funcs: Fns | ||
): (( | ||
...args: Parameters<Fns[T.Tail<Fns>['length']]> | ||
) => F.Return<T.Head<Fns>>) => { | ||
if (funcs.length === 0) { | ||
// infer the argument type so it is usable in inference down the line | ||
return <T>(arg: T) => arg | ||
// TODO: should probably throw an error here instead | ||
return (<T>(arg: T) => arg) as any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript really doesn't like this line, because it goes against the return type. Rather than introducing a union type, I just cast it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was able to make this type check in my implementation of I would go farther and say that passing compose a single function is also strange behavior and probably shouldn't be allowed, but I can see why you all might want to keep that due to the current behavior, whereas I doubt anyone has much use for or would miss it accepting 0 args. |
||
} | ||
|
||
if (funcs.length === 1) { | ||
|
@@ -100,3 +27,5 @@ export default function compose(...funcs: Function[]) { | |
|
||
return funcs.reduce((a, b) => (...args: any) => a(b(...args))) | ||
} | ||
|
||
export default compose |
Uh oh!
There was an error while loading. Please reload this page.