Relax type constraints for components passed to createRoutesStub
#14887
rossipedia
started this conversation in
Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Related bug: #14886
I think that
createRoutesStubshould allow any React component to be passed directly for theComponentproperty of a stubbed route.In specific implementation terms, that means:
interface StubRouteExtensions { - Component?: RouteComponentType; + Component?: React.ComponentType<any>; HydrateFallback?: HydrateFallbackType; ErrorBoundary?: ErrorBoundaryType; loader?: LoaderFunction; action?: ActionFunction; children?: StubRouteObject[]; meta?: MetaFunction; links?: LinksFunction; }Typescript is trying to be correct here, but I think in this situation it's not really viable:
createRoutesStubcan't realistically know every possibleRoute.ComponentTypefor every component that might be passed to it. So instead, it should accept any kind of component, and do its best to render it as a route component.This also lets "pre-typegen" prop-less route components that are currently passed directly into
createRoutesStubmigrate toRoute.ComponentPropswithout extensive hoop-jumping that would be required today.For example:
import { createRoutesStub } from "react-router"; import { test } from "vitest"; import ProductRoute from "~/routes/product"; - import type { Route } from "../.react-router/types/app/routes/+types/product"; test("it works", async () => { let Stub = createRoutesStub([ { id: "root", - Component: (props) => ( - <ProductRoute {...(props as Route.ComponentProps)} /> - ), + Component: ProductRoute, }, ]); });I initially tried going down the path of making
createRoutesStubgeneric, something likecreateRoutesStub<RouteConfig extends StubRouteObject[]>(), but it becomes a recursive type-definition problem, since you have to definechildrenas generic as well, and that type needs to extendStubRouteObject[]as well... which is currently being defined.While I think that approach might be solvable, it's a very complex bit of typescript-fu for very little benefit.
I also think that allowing
CreateRoutesStubto render any component is pretty useful in the general case.Most places where
createRoutesStubis used is for testing/verification (ie:vites/storybook/ etc...), and usually you have other things in place to give you signal that you got a prop definition wrong.Beta Was this translation helpful? Give feedback.
All reactions