【发布时间】:2018-08-20 11:07:14
【问题描述】:
所以我正在尝试使用 react router 4 和 Typescript 创建一个私有路由。这是我的代码:
type PrivateRouteProps<T> = T & { component: any, authRequired: boolean };
function PrivateRoute({ component: Component, authRequired, ...rest }: PrivateRouteProps<typeof rest>) {
return (
<Route
{...rest}
render={props => authRequired === true
? <Layout><Component {...props} /></Layout>
: <Redirect to={{ pathname: '/home', state: { from: props.location } }} />}
/>
);
}
我在 PrivateRoute 函数中遇到 ...rest arg 的问题,它给出了错误:
'rest' 隐式具有类型'any',因为它没有类型注释并且在其自己的初始化程序中直接或间接引用。
我查看了关于对象解构的 typescript 文档,但似乎找不到解决方案
【问题讨论】:
标签: reactjs typescript react-router