Skip to content

[@types/react-router-dom]: withRouter HoC produces compile error: "Property 'match' is missing in type '{}'." #17181

Description

@simonvizzini

I did a Google search and found two other people with the same problem, but no solution so far:
https://stackoverflow.com/questions/44118060/react-router-dom-with-typescript
microsoft/vscode#27235

Minimal code example:

const MyRouterComponent = withRouter(
  class MyComponent extends React.Component<RouteComponentProps<any>, any> {
    render() {
      return <div />;
    }
  }
)

class MainComponent extends React.Component<any, any> {
  render() {
    /* This line produces following compile error:
       error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<RouteComponentProps<any>, ComponentState...'.
          Type '{}' is not assignable to type 'Readonly<RouteComponentProps<any>>'.
            Property 'match' is missing in type '{}'.
    */
    return <MyRouterComponent />
  }
}

I understand what the problem is: MyRouterComponent expects props which I didn't explicitly pass, because withRouter will take care of that. So how does one use withRouter with TS correctly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions