关于使用withRouter破坏别名的打字稿警告

时间:2020-02-10 09:19:00

标签: typescript

我在React中有一个用纯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会发出警告,我上面的代码有什么问题?

0 个答案:

没有答案