Answer the question
In order to leave comments, you need to log in
Why am I getting error TS2322?
Hello! Let me show you the code first:
<Switch>
{
routes.map((route: IPrivateRoute | IRoute) => route.access // Здесь
? <PrivateRoute key={route.path} {...route}/> // и здесь появляется ошибка
: <Route key={route.path} to={route.path} render={
() => route.redirect ? <Redirect to={route.redirect} /> : route.component
} />)
}
</Switch>
export interface IRoute {
path: string
component?: typeof React.Component | React.FC
exact?: boolean
subRoutes?: Array<IRoute | IPrivateRoute>
redirect?: string
}
export interface IPrivateRoute extends IRoute {
component: typeof React.Component | React.FC
access: boolean
redirect: string
}
const PrivateRoute = ({component: Component, access, redirect, ...rest}: IPrivateRoute) => {
return (
<Route {...rest} render={props => access
? <Component {...props}/>
: <Redirect to={redirect}/>}
/>
)
}
export default PrivateRoute;
Answer the question
In order to leave comments, you need to log in
routes.map((route: IPrivateRoute | IRoute) => route.access // Здесь ошибка, так как в IRoute нет поля access, а значит нет и в юнионе IPrivateRoute | IRoute
? <PrivateRoute key={route.path} {...route}/> // а здесь, так как route.access не является тайп-гвардом, и не смотря на условие, тип по прежнему IPrivateRoute | IRoute
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question