首页 > 解决方案 > 关于使用 withRouter 破坏别名的打字稿警告

问题描述

我在反应中有一个用纯javascript编写的类组件:

class Auth extends Component {
  render() {
    const { component: Component, ...rest } = this.props //error: Property 'component' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'.ts(2339)


    const isAuth = getUser() //some function

    return (
      <Route
        {...rest}
        render={props =>
          isAuth ? (
            <Component {...props} />
          ) : (
            <Redirect
              to={{
                pathname: '/login'
              }}
            />
          )
        }
      />
    )
  }
}

export default withRouter(Auth) //error: Argument of type 'typeof Auth' is not assignable to parameter of type 'ComponentClass<RouteComponentProps<any, StaticContext, any>, any> | FunctionComponent<RouteComponentProps<any, StaticContext, any>> | (FunctionComponent<...> & ComponentClass<...>) | (ComponentClass<...> & FunctionComponent<...>)'.

但是 Typescript 发出警告,我上面的代码有什么问题?

标签: typescript

解决方案


推荐阅读