Skip to content
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

use ts-toolbelt for 'compose' types #3563

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-typescript2": "^0.24.0",
"rxjs": "^6.5.3",
"ts-toolbelt": "^3.8.63",
timdorr marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^3.5.3"
},
"npmName": "redux",
Expand Down
2 changes: 1 addition & 1 deletion src/applyMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down
91 changes: 10 additions & 81 deletions src/compose.ts
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
Expand All @@ -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>[]>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wanted to use ts-toolbelt's F.compose here, but it was swallowing types. This way, I can be more explicit and get an accurate return type.

...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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 as any, but I think it might make more sense to just throw an error instead. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to make this type check in my implementation of compose. However, as I suggested in comments on that PR, I think you're right that this is strange anyhow. However, I think it should simply be statically not allowed by not providing overloads for compose that take 0 arguments rather than allowing it and throwing a runtime error.

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) {
Expand All @@ -100,3 +27,5 @@ export default function compose(...funcs: Function[]) {

return funcs.reduce((a, b) => (...args: any) => a(b(...args)))
}

export default compose