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

Fix typings for compose function to accept single rest argument #1936

Merged
merged 1 commit into from
Sep 7, 2016
Merged

Fix typings for compose function to accept single rest argument #1936

merged 1 commit into from
Sep 7, 2016

Conversation

aikoven
Copy link
Collaborator

@aikoven aikoven commented Sep 7, 2016

Fixes #1935.

Cc. @ulfryk @Igorbek

@timdorr
Copy link
Member

timdorr commented Sep 7, 2016

Thanks!

@timdorr timdorr merged commit f8ec3ef into reduxjs:master Sep 7, 2016
@stephtr
Copy link

stephtr commented Sep 29, 2016

Without this commit the following code works:

import { compose, createStore, GenericStoreEnhancer, Reducer } from 'redux';
declare var enhancer1: GenericStoreEnhancer;
declare var enhancer2: GenericStoreEnhancer;
declare var reducer: Reducer<any>;
const finalCreateStore = compose(
  enhancer1,
  enhancer2
)(createStore)

const store = finalCreateStore(reducer);

Including the commit TypeScript now automatically selects function compose<{}>(...funcs: Function[]): (...args: any[]) => {}; instead of function compose<A, T1, R>( f1: (b: A) => R, f2: Func1<T1, A> ): Func1<T1, R>;, which results in Type '{}' has no compatible call signatures.
But shouldn't TypeScript select the first appropriate overload definition?

@aikoven
Copy link
Collaborator Author

aikoven commented Oct 4, 2016

@stephtr Filed an issue in TypeScript repo: microsoft/TypeScript#11343

@stephtr
Copy link

stephtr commented Oct 5, 2016

Would it maybe be wise to revert this commit until the TypeScript issue is somehow solved? Because code written with the definition of Redux 3.6.0 (like the one above) needs to be changed to compose<Redux.StoreEnhancerStoreCreator<...>>(. However this line does not work with the 3.6.0 version of index.d.ts.

@aikoven
Copy link
Collaborator Author

aikoven commented Oct 7, 2016

@stephtr How about compose(...) as Redux.StoreEnhancerStoreCreator<...>?

@stephtr
Copy link

stephtr commented Oct 7, 2016

In that case it has to be compose(...) as Redux.StoreEnhancer<...>. I just tested it, works in 3.6.0 and master (even though using no typecast was nicer).

@Igorbek
Copy link
Contributor

Igorbek commented Oct 7, 2016

I just wanted to make a note what a technical challenge is to properly type compose or, more importantly, a store enhancing mechanism in TypeScript.
In general, an enhancer is just a function which may have it's own assumptions of what is the store and what will be a new enhanced store it would construct. I'll list just a very obvious aspects:

  • an enhancer can expect a specific state type (or its specific capabilities) or work with any generic type
  • an enhancer can modify the dispatcher's signature (add a new overload or even completely substitute the signature) - it's not just additive (& type operator cannot be used in general)
    • a new signature can be constructed in very generic way by using type combinators

I was thinking about this and tried to prototype type flowing (state type, dispatch type) through enhancers composition and came up with a suggestion for the TypeScript team which seems very complicated even for just starting discussion :) I described the challenge and possible solution here microsoft/TypeScript#10247 please come there and bug TS team to pick it up for a design meeting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants