react-router-dom and Typescript: A type annotation is necessary.ts(2742)

Viewed 1117

I'm trying to route a class component with react-router-dom. When I use withRouter on my component, I get this error:

The inferred type of 'default' cannot be named without a reference to 'react-transition-group/node_modules/@types/react'. This is likely not portable. A type annotation is necessary. ts(2742)

Example code:

/* eslint-disable react/prefer-stateless-function */
import React from "react";
import { RouteComponentProps, withRouter } from "react-router-dom";

type Params = {
  value: string;
};

type Props = RouteComponentProps<Params>;

class ClassComponent extends React.Component<Props> {
  render() {
    const { match } = this.props;
    return <span>Value: {match.params.value}</span>;
  }
}

export default withRouter(ClassComponent);

What do I need to annotate and how?

1 Answers

I fixed this by deleting the node_modules folder and re-downloading all dependencies.

Related