简体   繁体   English

类型 &#39;(props: RouteProps) =&gt; JSX.Element&#39; 不可分配给类型 &#39;PropsWithChildren<RouteProps> &#39;

[英]Type '(props: RouteProps) => JSX.Element' is not assignable to type 'PropsWithChildren<RouteProps>'

I am using typescript with react I wanted to render a component but getting typescript error我正在使用带有反应的打字稿我想渲染一个组件但收到打字稿错误

Type '(props: RouteProps) => JSX.Element' is not assignable to type 'PropsWithChildren'.类型 '(props: RouteProps) => JSX.Element' 不可分配给类型 'PropsWithChildren'。
Property 'component' is missing in type '(props: RouteProps) => JSX.Element' but required in type 'RouteProps'.类型“(props: RouteProps) => JSX.Element”中缺少属性“component”,但在“RouteProps”类型中需要。

 import React, { ReactNode, PropsWithChildren } from "react";
 import { Route, Redirect } from "react-router";
 
  export interface RouteProps {
     component: FunctionComponent;
   }


 interface PrivateRoutesProp {
   // component: PropsWithChildren<RouteProps> // not works  
     // also having "noImplicitAny": true, [this was mandatory]
   component: PropsWithChildren<???>; //what i have to use here,object is not working
   exact: boolean;
  path: string;
 }

const PrivateRoute = ({ component: Component, ...rest }: PrivateRoutesProp) => (
  <Route
   {...rest}
  render={(props) => (isLoggedIn ? <Component {...props} /> : <Redirect to="/" />)}
  />
  );

 export default PrivateRoute;

can anyone help me out of this谁能帮我解决这个问题

PropsWithChildren just type PropsWithChildren<P> = P & { children?: ReactNode }; PropsWithChildren 只需type PropsWithChildren<P> = P & { children?: ReactNode }; It should include props of Route and custom props.它应该包括 Route 和自定义道具的道具。

Here my idea:这是我的想法:

export interface PrivateRoutesProp {
    component: FunctionComponent;
    exact: boolean;
    path: string;
    isLogin: boolean
}

const PrivateRoute = ({ component, isLogin, ...rest }: PropsWithChildren<PrivateRoutesProp & RouteProps>) => (
    <Route
        {...rest}
        render={(props) => (isLogin ? React.createElement(component, props) : <Redirect to="/" />)}
    />
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "类型 &#39;({ 文章 }: Props) =&gt; JSX.Element&#39; 不可分配给类型 &#39;NextPage&lt;{}, {}&gt;&#39;" - Type '({ articles }: Props) => JSX.Element' is not assignable to type 'NextPage<{}, {}>' 类型 &#39;JSX.Element&#39; 不可分配给类型 &#39;Element&#39; - Type 'JSX.Element' is not assignable to type 'Element' 类型 '() =&gt; JSX.Element' 不可分配给类型 'ReactNode' - Type '() => JSX.Element' is not assignable to type 'ReactNode' JSX.Element 不可分配给 ForwardRefRenderFunction 类型的参数 - JSX.Element is not assignable to parameter of type ForwardRefRenderFunction JSX.Element' 不可分配给类型 'FunctionComponent&lt;{}&gt;' - JSX.Element' is not assignable to type 'FunctionComponent<{}>' '(props: ITableProps) =&gt; JSX.Element' 类型的参数不能分配给类型的参数...... - React HOC - Argument of type '(props: ITableProps) => JSX.Element' is not assignable to parameter of type … - React HOC “tech”类型的参数不能分配给“PropsWithChildren”类型的参数<props> '</props> - Argument of type '“tech”' is not assignable to parameter of type 'PropsWithChildren<Props>' 使用自定义条件渲染组件时,类型 JSX.Element 不可分配给类型 React.ReactNode - Type JSX.Element is not assignable to type React.ReactNode when using customed conditional render component 输入 '({ items }: PropsWithChildren<todoprops> ) => Element[]' 不可分配给类型 'FunctionComponent<todoprops> '</todoprops></todoprops> - Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>' 如何输入窄 JSX.Element - How to type narrow JSX.Element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM