首页 > 解决方案 > react-router-dom 和 Typescript:需要类型注释。ts(2742)

问题描述

我正在尝试使用 react-router-dom 路由类组件。当我withRouter在我的组件上使用时,我收到此错误:

如果没有对 的引用,则无法命名推断的“默认”类型 'react-transition-group/node_modules/@types/react'。这可能是不可移植的。类型注释是必要的。ts(2742)

示例代码:

/* 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);

我需要注释什么以及如何注释?

标签: reactjstypescriptreact-router-dom

解决方案


我通过删除 node_modules 文件夹并重新下载所有依赖项来解决此问题。


推荐阅读