-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
add TypeScript typings #541
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
Conversation
Obviously that didn't happen...
Continuing on previous thread: I agree that having a combination of each allowed type for each overload makes too much bloat. I think we can omit the case when But for the case when second argument is object, I think we must include another signature, because in this case There are a few other things we could improve, but I don't have enough time right now for a thorough review. Will check back in a couple days. |
We ask Not sure I understand. And I tried function connect<State, TOwnProps, TStateProps, TDispatchProps extends MapDispatchToPropsObject>(
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>|null,
mapDispatchToProps: FuncOrSelf<TDispatchProps>,
): ComponentDecorator<TStateProps & TDispatchProps & TOwnProps, TOwnProps>; can' t get past
TS won't associate interface DispatchProps {
someAction: any
} with interface MapDispatchToPropsObject {
[name: string]: ActionCreator<any>;
} |
…select'. (6 failing tests to go)
…ops pass. BREAKING CHANGE: requires setting an option parameter mapStateIsFactory/mapDispatchIsFactory to signal factory methods.
…hind-the-scenes behavior of new implementation.
@aikoven I narrowed allowed Actions. I am quite satisfied with the result. It fixes your test failure (i.e. it now fails). There is a very tiny little hack for Thunks (we provide the wrong return type if TDispatchProps is not provided, covariantly compatible of course), but it is going to make everyone life's so much easier I think it is a must. I added tests around it to be sure that behavior is perfect in any cases. Have a look and tell me what you think. |
Good work! I'm not sure that Also, since we're working on the |
@timdorr Why is it closed? Should we stop our effort to release the typings inside react-redux repo? |
I think this was auto closed by my adjusting of the branches on the repo. The next branch was deleted, so that's probably why. I think we just need to change the branch and then I can open it. I'm on mobile at the moment, but will do that soon. |
@timdorr Awesome thanks! |
@bbenezech Why don't we use Partial type (introduced in Typescript 2.1) for annoying TOwnProps, TStateProps, TDispatchProps things? we can just pass two type arguments to connect: TState and TProps. |
@rokoroku What's your intended use of |
@aikoven We can simplify things. Writing types for every TOwnProps, TStateProps, TDispatchProps is really cumbersome, and Partial<T> is very useful for reducing them. For example, with given redux stateinterface ReduxState {
todos: TodoItem[];
}; suggested (with partial)interface AppProps {
todos: TodoItem[];
addTodo: typeof TodoActions.addTodo;
};
interface AppState {
/* something */
};
@connect<ReduxState, AppProps>(
(state) => ({ todos: state.todos }),
(dispatch) => ({ addTodo: bindActionCreators(TodoActions.addTodo, dispatch) })
)
export default class App extends React.Component<AppProps, AppState>{
...
}
// Perhaps it will be similar to this.
@connect<ReduxState, Partial<AppProps>, Partial<AppProps>, Partial<AppProps>>( ... ) current (without partial)interface AppOwnProps {
todos: TodoItem[];
}
interface AppStateProps {
todos: TodoItem[];
}
interface AppDispatchProps {
addTodo: typeof TodoActions.addTodo;
};
interface AppState {
/* something */
};
@connect<ReduxState, AppOwnProps, AppStateProps, AppDispatchProps>(
(state) => ({ todos: state.todos }),
(dispatch) => ({ addTodo: bindActionCreators(TodoActions.addTodo, dispatch) })
)
export default class App extends React.Component<AppOwnProps & AppStateProps & AppDispatchProps, AppState>{
...
} (Note: above code is just an example.) |
@rokoroku It's not really accurate. interface AppProps {
todos: TodoItem[];
addTodo: typeof TodoActions.addTodo;
};
@connect<ReduxState, AppProps>(
(state) => ({ todos: state.todos }),
(dispatch) => ({ addTodo: bindActionCreators(TodoActions.addTodo, dispatch) })
)
export default class App extends React.Component<AppProps, AppState>{
...
} In this case, props of connected component (the one returned from Btw, TypeScript doesn't yet work well with decorators that change the type of decorated stuff, which To avoid having to set all generic arguments, you can use this: connect(
(state: MyState, props: MyProps) => {...},
) i.e. let TypeScript infer everything for you by just specifying types for |
@aikoven However, we already set strict type here: Can't we add some abbreviated definitions while preserving existing definitions? |
Ohh, I figured out your intentions. I focused on the inside of the component (where our logic will be written), and you are likely paying attention to the Considering the result of I might have been wrong, but I just wanted to share my thoughts :) |
Is there any reason why connectAdvanced is not included in the typings? |
The current typings on DefinitelyTyped do not expose the connected components props on the connected component, breaking type checking when you have to pass some additional props. Does anyone have a workaround for that? Are the typings in this branch usable? |
@theduke They are totally usable and include a lot of improvements over the DT ones. declare module "react-redux" {
<typings here>
} and remove the @types/react-redux package. It may change a bit before release, but unless you have thousands of connected components, this should not be of concern. @maclockard lack of time, sadly. I do not use this part of the API, I have no time to investigate atm. |
@bbenezech thanks for the update, glad to hear. |
Hi @bbenezech: We are using react-redux with Typescript in one of our projects and would like to help in any way we can to get this merged. Let me know if you need any help. |
Closing in favor of #815 |
Follow up to #538
Rebased against next