【问题标题】:rest implicitly has type any because it does not have a type annotation and is referenced directly or indirectly in its own initializerrest 隐式具有类型 any 因为它没有类型注释并且在其自己的初始化程序中直接或间接引用
【发布时间】: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


    【解决方案1】:

    我认为你不能这样做 typeof rest 因为它不知道 rest 是什么类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2018-10-29
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多