6

I am starting to learn typescript and the is a behaviour that I don't understand.

I got this error:

Type 'ComponentClass<{}>' is not assignable to type 'StatelessComponent<void | RouteComponentProps<any>> | ComponentClass<void | RouteComponentProps<a...'.
  Type 'ComponentClass<{}>' is not assignable to type 'ComponentClass<void | RouteComponentProps<any>>'.
    Type '{}' is not assignable to type 'void | RouteComponentProps<any>'.
      Type '{}' is not assignable to type 'RouteComponentProps<any>'.

enter image description here

Here is my App component :

interface AppProps extends React.Props<void> {
  todos: TodoItemData[];
  actions: typeof TodoActions;
};

interface AppState {
  /* empty */
}

class App extends React.Component<AppProps, AppState>{
  render() {return (<div></div>);
  }
}

function mapStateToProps(state: RootState) {
  return {
    todos: state.todos
  };
}

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(TodoActions as any, dispatch)
  };
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

If I change in the declaration of my App component AppProps by void or React.Props I got no errors but I always have one with AppProps.

I don't understang why it's not working, AppProps is extending from React.Props. Did you see the mistake ?

2
  • React-router v4 typings are pretty new. I see that they use something like RouteComponentProps interface in props. To proper use them check their tests - github.com/DefinitelyTyped/DefinitelyTyped/tree/master/… Commented Mar 24, 2017 at 17:20
  • It would help (and clarify) if you specified which version of react-router you're using. Commented Mar 24, 2017 at 19:06

3 Answers 3

2

I ran into a similar problem with [email protected].

I fixed it by extending my AppProps interface with RouteComponentProps interface, so in your case the AppProps interface would look like this:

import { RouteComponentProps } from 'react-router';
...

interface AppProps extends RouteComponentProps<any> {
  todos: TodoItemData[];
  actions: typeof TodoActions;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Based on this answer on GitHub, this may work:

export default connect< AppProps, {}, {} >(
  mapStateToProps,
  mapDispatchToProps
)(App);

Comments

0

This has to do with react-router and redux you have to add types to your connect function:

interface AppProps {
  todos: TodoItemData[];
};

interface DispatchProps { 
  actions: typeof TodoActions;
}


interface AppState {
  /* empty */
}

class App extends React.Component<AppProps & DispatchProps & RouteComponentProps, AppState>{
  render() {return (<div></div>);
  }
}

function mapStateToProps(state: RootState) {
  return {
    todos: state.todos
  };
}

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(TodoActions as any, dispatch)
  };
}

export default connect<AppProps, DispatchProps, RouteComponentProps<any>(
  mapStateToProps,
  mapDispatchToProps
)(App);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.