You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hook results take the caller's expected type when no select is passed (useLoaderData, useSearch, ...) #8666
Without select, the hooks' TSelected is inferred from the call site's expected type, so the hook "returns" whatever the caller wants and wrong usage typechecks:
constpostsRoute=createRoute({getParentRoute: ()=>rootRoute,path: '/posts',loader: ()=>({posts: ['first post']}),})constdata=postsRoute.useLoaderData()consta: number=data// error, as expectedconstb: number=postsRoute.useLoaderData()// no errortakesNumber(postsRoute.useLoaderData())// no error
The result types are unknown extends TSelected ? Resolved : TSelected. TypeScript infers type arguments from the contextual return type too, and with no select that is the only inference site, so TSelected becomes number.
Affected: useLoaderData, useSearch, useParams, useRouteContext, useLoaderDeps, useMatch, useLocation, useRouterState, useMatches / useParentMatches / useChildMatches (the route-bound and getRouteApi forms too), in react, solid and vue.
We hit this when passing Route.useLoaderData() straight into a prop of a different type; it typechecked and only misbehaved at runtime.
Activity