与typescript一起使用时应对路由器问题

与typescript一起使用时应对路由器问题,typescript,react-router,Typescript,React Router,我正在使用typescript和react路由器(5.2.0) 我有一个需要匹配参数(id)的组件 import React,{useffect,useState}来自“React” 从“react router dom”导入{RouteComponentProps} 从“../../data/users”导入用户 类型ShowParams={id:string} 功能展示(道具:RouteComponentProps){ const id=props.match.params.id 返回{id

我正在使用typescript和react路由器(5.2.0)

我有一个需要匹配参数(id)的组件

import React,{useffect,useState}来自“React”
从“react router dom”导入{RouteComponentProps}
从“../../data/users”导入用户
类型ShowParams={id:string}
功能展示(道具:RouteComponentProps){
const id=props.match.params.id
返回{id}
}
导出默认显示
这在模板化RouteComponentProps时效果很好,但这会导致路由声明出现问题 一旦我在Show中正确定义了道具,路由声明就会在组件上显示一个错误

<Route path="/companies/:id" exact={true}  component={ShowCompany} />

Overload 1/2'(props:Readonly):Route'给出了以下错误。
类型“(props:RouteComponentProps)=>元素|未定义”不可分配给类型“ComponentClass | FunctionComponent | ComponentClass | FunctionComponent |未定义”。
类型“Element | undefined”不可分配给类型“ReactElement | null”。
类型“undefined”不可分配给类型“ReactElement | null”。
重载2/2'(道具:RouteProps,上下文?:any):Route'给出了以下错误。
类型“(props:RouteComponentProps)=>元素|未定义”不可分配给类型“ComponentClass | FunctionComponent | ComponentClass | FunctionComponent |未定义”。
类型“(props:RouteComponentProps)=>元素|未定义”不可分配给类型“FunctionComponent”。TS2769
这是非常有意义的,因为它不再能够在没有props声明的情况下仅仅渲染组件


如何在routes中声明道具类型

我想知道这是否是一个已修复的bug?我开始使用useParams钩子,而不是通过道具加载,所以我不知道
<Route path="/companies/:id" exact={true}  component={ShowCompany} />
  Overload 1 of 2, '(props: Readonly<RouteProps>): Route<RouteProps>', gave the following error.
    Type '(props: RouteComponentProps<ShowParams, StaticContext, PoorMansUnknown>) => Element | undefined' is not assignable to type 'ComponentClass<any, any> | FunctionComponent<any> | ComponentClass<RouteComponentProps<any, StaticContext, PoorMansUnknown>, any> | FunctionComponent<...> | undefined'.
 Type 'Element | undefined' is not assignable to type 'ReactElement<any, any> | null'.
          Type 'undefined' is not assignable to type 'ReactElement<any, any> | null'.
  Overload 2 of 2, '(props: RouteProps, context?: any): Route<RouteProps>', gave the following error.
    Type '(props: RouteComponentProps<ShowParams, StaticContext, PoorMansUnknown>) => Element | undefined' is not assignable to type 'ComponentClass<any, any> | FunctionComponent<any> | ComponentClass<RouteComponentProps<any, StaticContext, PoorMansUnknown>, any> | FunctionComponent<...> | undefined'.
      Type '(props: RouteComponentProps<ShowParams, StaticContext, PoorMansUnknown>) => Element | undefined' is not assignable to type 'FunctionComponent<any>'.  TS2769