-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Provider API breaks strict typing #290
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
Comments
You probably confuse it with import { connect } from 'react-redux'
class App extends Component { }
export default connect(
mapStateToProps
)(App) I don’t know enough about TypeScript but I imagine you could write bindings for |
Thanks for clarification. I use TypeScript type definitions from TSD repository. It works great with JSX. Consider following example: export default class App extends Component<AppProps, AppState> { render(){/*...*/} } which interface AppProps {
todo: Array<{}>
} This TSX code will complain that import {render} from 'react';
import App from './app';
render(<App />, document.body); Now if I use redux import {connect} from 'react-redux';
class App extends Component<AppProps, AppState> { render(){/*...*/} }
export default connect(state=> {todos: state.todos})(App) The TSX compiler will not understand that This is most likely due to type definition or TSX capabilities. I just wanted to leave this write up for future references. I'll post here if I could figure it out although I'm pretty new to React, Redux and TypeScript! |
The problem is likely that those typings don’t tell TypeScript that parent props are going to be merged with what you return from Related: reduxjs/redux#1401. |
@mohsen1 I'm using custom typings for export interface ClassDecorator<P, O> {
(component: StatelessComponent<P>): ComponentClass<O>;
}
export function connect<O, S, D>(
mapStateToProps?: MapStateToProps<O, S>,
mapDispatchToProps?: MapDispatchToPropsFunction<O, D>|D
): ClassDecorator<O & S & D, O>;
export function connect<O, S, D, P>(
mapStateToProps: MapStateToProps<O, S>,
mapDispatchToProps: MapDispatchToPropsFunction<D, O>|D,
mergeProps: MergeProps<S, D, O, P>,
options?: Options): ClassDecorator<P, O>;
export interface MapStateToProps<O, S> {
(state: any, ownProps?: O): S;
}
export interface MapDispatchToPropsFunction<O, D> {
(dispatch: Dispatch, ownProps?: O): D;
}
export interface MergeProps<S, D, O, P> {
(stateProps: S, dispatchProps: D, ownProps: O): P;
} They only work with stateless functional components and don't cover all cases, but types are inferred correctly: const App = connect(
selector,
{
actionCreator,
}
)(props => {
// `props` type is inferred from return type of `selector`
// and shape of action creators object
}) The major drawback that kept me from submitting these to DefinitelyTyped is that TypeScript doesn't understand decorators that change type. Before that I had to explicitly specify |
@aikoven how is Angular2 solving it with their decorators? |
@mikekidder I'm not aware of it, could you please point to some examples? |
@aikoven can you share your full |
@gaearon - I also had this problem, but did not found any good solution. It's actually problem of how @mikekidder - Angular2 decorators are not useful here. Their way is binding a property of a class to input attr. If something similar was made for react it would look like this:
and now |
Don't suppose this has been resolved nicely yet for decorating React components defined as classes? |
@codeandcats Nope, but there are some issues in TS repo to track, e.g.: microsoft/TypeScript#4881 |
I tried out #433 and those typings work as expected for me. The return type of interface ComponentDecorator<TOriginalProps, TOwnProps> {
(component: ComponentClass<TOriginalProps>|StatelessComponent<TOriginalProps>): ComponentClass<TOwnProps>;
} which is exactly what we want! |
@Zalastax it looks like that PR has been closed. 😢 I don't suppose you feel like forking and re-requesting it? 🌹 |
The
Provider
API magically provides props for a component that requires those props. For example if we have a regular React code before provider is used like this:After using
Provider
we change it to following:this is not a problem in regular JS or JSX, but when I use TypeScript, the compiler complains that
App
requires required proptodos
. Of course I can make it an optional property but that's not optimal.The text was updated successfully, but these errors were encountered: