Reactjs 如何为react专用路由器组件添加类型?

Reactjs 如何为react专用路由器组件添加类型?,reactjs,typescript,react-router,react-router-v4,Reactjs,Typescript,React Router,React Router V4,我想为身份验证用户添加专用路由器。 并找到使用专用路由器组件的简单解决方案,但typescript返回我类型错误。我尝试添加任何类型,但没有帮助。如何在此处添加类型 Error:(18, 18) TS2322: Type '{ exact: true; path: string; component: typeof Main; }' is not assignable to type 'IntrinsicAttributes & Component<any, any, any>

我想为身份验证用户添加专用路由器。 并找到使用专用路由器组件的简单解决方案,但typescript返回我类型错误。我尝试添加任何类型,但没有帮助。如何在此处添加类型

Error:(18, 18) TS2322: Type '{ exact: true; path: string; component: typeof Main; }' is not assignable to type 'IntrinsicAttributes & Component<any, any, any>'.
  Property 'exact' does not exist on type 'IntrinsicAttributes & Component<any, any, any>'.
路由器

私家侦探

当你像任何人一样使用类型时,你正在破坏typescript的味道

现在,看看您的代码,我认为您希望根据props中auth的值将用户带到特定路径。所有剩下的道具都应该和react router一样

我在代码中看到的一个明显的错误是,在函数组件中,每个道具都作为参数接收,但它只接收一个参数,即道具对象

所以你的方法应该是

const PrivateRoute = (props: any): any => ()
现在,一个更干净、更好的方法是:

从“React”导入React,{Component}; 从“react router dom”导入{RouteProps,Route,Redirect}; 导出接口PrivateRouteProps扩展RouteProps{ auth:boolean | null } 函数PrivateRoute{component:component,auth,…rest}:PrivateRouteProps{ 回来 验证!==true ? : } /> } 当你像任何人一样使用类型时,你正在破坏typescript的味道

现在,看看您的代码,我认为您希望根据props中auth的值将用户带到特定路径。所有剩下的道具都应该和react router一样

我在代码中看到的一个明显的错误是,在函数组件中,每个道具都作为参数接收,但它只接收一个参数,即道具对象

所以你的方法应该是

const PrivateRoute = (props: any): any => ()
现在,一个更干净、更好的方法是:

从“React”导入React,{Component}; 从“react router dom”导入{RouteProps,Route,Redirect}; 导出接口PrivateRouteProps扩展RouteProps{ auth:boolean | null } 函数PrivateRoute{component:component,auth,…rest}:PrivateRouteProps{ 回来 验证!==true ? : } /> } 谢谢但是现在我犯了一个错误:16,20 TS2604:JSX元素类型“Component”没有任何构造或调用签名。谢谢!但现在我犯了一个错误:16,20 TS2604:JSX元素类型“Component”没有任何构造或调用签名。
const PrivateRoute = (props: any): any => ()