I have written this package (https://www.npmjs.com/package/react-wise-router) which I use for some of my projects, and decided to upgrade to using TS.
The problem is that some of the props I pass in are all grabbed as rest props which then is passed to another child component. My component's prop types:
interface WiseRouterProps {
component: React.ComponentType<any> | null | undefined,
needsAuthentication: boolean | undefined,
needsAuthorisation: boolean | undefined,
isAuthenticated: boolean | undefined,
isAuthorised: boolean | undefined,
userPermissions: string[] | undefined,
routePermissions: string[] | undefined,
redirectTo: string | null | undefined,
defaultRedirect: string | '/' | undefined,
fallback: null | Fallback | undefined,
debug: boolean | undefined,
passRouteProps: boolean | string[] | undefined,
rest: [] | null | undefined;
}
If I pass in a prop such as exact or path which are not used by my component, and are only passed to a child component as rest, I get an error such as:
Type '{ exact: boolean; path: string; key: string; isAuthenticated: any; needsAuthentication: boolean; needsAuthorisation: boolean; routePermissions: never[]; userPermissions: any; passRouteProps: boolean; ... 4 more ...; debug: true; }' is not assignable to type 'IntrinsicAttributes & WiseRouterProps & { children?: ReactNode; }'.
Property 'exact' does not exist on type 'IntrinsicAttributes & WiseRouterProps & { children?: ReactNode; }'.
Which I think is logical, but I am grabbing every prop that is defined in my interface, and leave everything else as rest.
This is how I am grabbing my props including rest props:
const WiseRouter: React.FC<WiseRouterProps> = (props) => {
const { component: Component = null, isAuthenticated, isAuthorised, needsAuthentication,
needsAuthorisation, routePermissions,
userPermissions, redirectTo,
defaultRedirect = '/', fallback,
debug = false, passRouteProps = false, ...rest } = props;
//...
}
I have been having this issue for some time now, and I would appreciate if someone could explain this to me. Thank you!
Edit: added code
import { BrowserRouter as Router, Switch, Redirect } from "react-router-dom";
// Routes is an array of objects defining each route
<Router>
<Switch>
{Routes.map(route => (
<WiseRouter
exact={true}
path={route.path}
key={route.name}
isAuthenticated={isAuthenticated}
needsAuthentication={route.needsAuthentication}
needsAuthorisation={route.needsAuthorisation}
routePermissions={route.permissions}
userPermissions={permissions}
passRouteProps={!!route.passRouteProps}
redirectTo={route.redirectTo}
defaultRedirect='/'
component={route.component}
fallback={route.fallback}
debug={true} />
))}
<Redirect from="*" to="/http-status/404" />
</Switch>
</Router>
type WiseRouteProps = RouteProps & YourCustomPropsWiseRouteinstead ofWiseRouter? It's not actually routing, is it? Also, the parameters you cannot pass, are you passing them to thecomponentprop? Do you know the types of routes you have? Because then you could do something similar as I have described in my answer.