TypeScript React Functional Component - is missing the following properties from type 'Element': type, props, key error

Viewed 4645

I have the following definitions:

IRoute:


export interface IRoute{
    key: string, 
    path: string,
    exact: boolean,
    component: JSX.Element,
    private?: boolean
}

IRoutesConfig:

import { IRoute } from "./IRoute";

export default interface IRoutesConfig {
    [key : string] : [IRoute]
}

and my Routes Configration, that I will then pass on to a class for further enhancements

import Login from "../container/Auth/Login/Login";
import IRoutesConfig from "./IRoutesConfig";

const Routes : IRoutesConfig = {
    "Auth": [
        {
            key: "login-page",
            exact: true, 
            component: Login,
            path: "/auth/login"
        }
    ]
}

export default Routes;

I am testing with a Login functional component:

import React, { FC, ReactElement } from 'react';

type LoginProps = {

}

const Login : FC<LoginProps> = (props) : ReactElement => {
  return (
    <>
    
    </>
  );
}

export default Login;

This is a new project setup and there isn't anything defined yet for the Login Props. So it is currently left empty and will be extended later on, same goes for the content, as we are currently setting up the routes architecture.

VSCode is giving me the following hint over "component" in Routes:IRoutesConfig:

vscode error

Of course I am not declaring type, props and key.

At the end the routes config will be given to a RouteManager, that will dynamically create Routes for the Switch of `React-Router-Dom', plus some other stuff, depending on where I access it.

It will create a map of routes such as:

<Route key="login-page" path="/auth/login" component={Login} />

Which is then used in a react container component:

...

let authRoutes = RoutesManager.getAllRoutes("Auth");

return (
    <Switch>
      {authRoutes}
      <Redirect key="account-redirect" to="/account" />
    </Switch>
  );

Question: From my understanding, the react-router-dom route expects in it's arg component a JSX.element, however the return type of my Login functional component is a ReactElement. JSX.element is a generic ReactElement in this context, as far as I understood it. The Login FC will have children.

How can I solve the issue of passing the Login FC to the config correctly, to be later used by the route component of react-router-dom?

1 Answers

As I understood the error is from component: JSX.Element. And you're trying to create protected routes by adding a private property to Route. You can change your IRoute interface to extend the react-router defined types so you won't face this issue enay more.

First: Add this import statement.

import { RouteProps } from "react-router-dom";

Second: change your IRoute inteface to this:

export interface IRoute extends RouteProps{
    private?: boolean
}

In this way, You simply telling that my IRoute has all of the simple Route properties plus private.

Related