Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding element 'Component' implicitly has an 'any' type.ts [duplicate]

Newbie here. I have problems with the following code. I want to define a protected "Dashboard" page, as follows, but is giving me this binding error:

Binding element 'Component' implicitly has an 'any' type.ts(7031)


import * as React from "react";
import { Route, Router, Redirect } from "react-router-dom";
import Dashboard from "../features/dashboard";
import LoginContainer from "../features/login/containers/LoginContainer";
import SignUpContainer from "../features/signup/containers/SignUpContainer";
import { createBrowserHistory } from "history";

const history = createBrowserHistory();

export default () => (
  <Router history={history}>
    <Route path="/" component={LoginContainer} />
    <Route path="/login" component={LoginContainer} />
    <Route path="/register" component={SignUpContainer} />

    <PrivateRoute path="/dashboard" component={Dashboard} />
  </Router>
);

function PrivateRoute({ component: Component, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        localStorage.getItem("MyStoredUser") ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login"
            }}
          />
        )
      }
    />
  );
}

Basically I am using the same code as it is here:Protected routes and authentication with React Router v4 and eact Router - Redirects

What am I doing wrong?

Error Img

like image 609
Hunor Avatar asked Aug 06 '19 08:08

Hunor


1 Answers

You havn't defined any types, and you apparently have typescript configured to forbid implicit any in arguments. You'll need to add in types to satisfy the typescript compiler:

import * as React from 'react';
import {Route, Router, Redirect, RouteProps} from 'react-router';

const PrivateRoute: React.FC<RouteProps> = ({ component: Component, ...rest }) => {
  return (
    // ... etc
  );
}
like image 78
Nicholas Tower Avatar answered Nov 01 '22 12:11

Nicholas Tower