Skip to content

Commit

Permalink
fix: omit RootSearchSchema from search param type (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel authored Jan 17, 2024
1 parent 4325b07 commit 46db31b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
31 changes: 14 additions & 17 deletions packages/react-router/src/Matches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import warning from 'tiny-warning'
import { CatchBoundary, ErrorComponent } from './CatchBoundary'
import { useRouter, useRouterState } from './RouterProvider'
import { ResolveRelativePath, ToOptions } from './link'
import { AnyRoute, ReactNode } from './route'
import { AnyRoute, ReactNode, RootSearchSchema } from './route'
import {
ParseRoute,
RouteById,
Expand All @@ -13,7 +13,7 @@ import {
RoutePaths,
} from './routeInfo'
import { RegisteredRouter, RouterState } from './router'
import { GetTFrom, NoInfer, StrictOrFrom, pick } from './utils'
import { NoInfer, StrictOrFrom, pick } from './utils'

export const matchContext = React.createContext<string | undefined>(undefined)

Expand All @@ -36,7 +36,10 @@ export interface RouteMatch<
loaderData?: RouteById<TRouteTree, TRouteId>['types']['loaderData']
routeContext: RouteById<TRouteTree, TRouteId>['types']['routeContext']
context: RouteById<TRouteTree, TRouteId>['types']['allContext']
search: RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema']
search: Exclude<
RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema'],
RootSearchSchema
>
fetchCount: number
abortController: AbortController
cause: 'preload' | 'enter' | 'stay'
Expand Down Expand Up @@ -291,14 +294,12 @@ export function getRenderedMatches(state: RouterState) {
}

export function useMatch<
TOpts extends StrictOrFrom<TFrom>,
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TFromInferred extends RouteIds<TRouteTree> = GetTFrom<TOpts, TRouteTree>,
TRouteMatchState = RouteMatch<TRouteTree, TFromInferred>,
TRouteMatchState = RouteMatch<TRouteTree, TFrom>,
TSelected = TRouteMatchState,
>(
opts: TOpts & {
opts: StrictOrFrom<TFrom> & {
select?: (match: TRouteMatchState) => TSelected
},
): TSelected {
Expand Down Expand Up @@ -377,17 +378,15 @@ export function useParentMatches<T = RouteMatch[]>(opts?: {
}

export function useLoaderDeps<
TOpts extends StrictOrFrom<TFrom>,
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TFromInferred extends RouteIds<TRouteTree> = GetTFrom<TOpts, TRouteTree>,
TRouteMatch extends RouteMatch<TRouteTree, TFromInferred> = RouteMatch<
TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
TRouteTree,
TFromInferred
TFrom
>,
TSelected = Required<TRouteMatch>['loaderDeps'],
>(
opts: TOpts & {
opts: StrictOrFrom<TFrom> & {
select?: (match: TRouteMatch) => TSelected
},
): TSelected {
Expand All @@ -402,17 +401,15 @@ export function useLoaderDeps<
}

export function useLoaderData<
TOpts extends StrictOrFrom<TFrom>,
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TFromInferred extends RouteIds<TRouteTree> = GetTFrom<TOpts, TRouteTree>,
TRouteMatch extends RouteMatch<TRouteTree, TFromInferred> = RouteMatch<
TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
TRouteTree,
TFromInferred
TFrom
>,
TSelected = Required<TRouteMatch>['loaderData'],
>(
opts: TOpts & {
opts: StrictOrFrom<TFrom> & {
select?: (match: TRouteMatch) => TSelected
},
): TSelected {
Expand Down
10 changes: 4 additions & 6 deletions packages/react-router/src/useParams.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { AnyRoute } from './route'
import { RouteIds, RouteById, AllParams } from './routeInfo'
import { RouteIds, RouteById } from './routeInfo'
import { RegisteredRouter } from './router'
import { last } from './utils'
import { useRouterState } from './RouterProvider'
import { StrictOrFrom, GetTFrom } from './utils'
import { StrictOrFrom } from './utils'
import { getRenderedMatches } from './Matches'

export function useParams<
TOpts extends StrictOrFrom<TFrom>,
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TFromInferred = GetTFrom<TOpts, TRouteTree>,
TParams = RouteById<TRouteTree, TFromInferred>['types']['allParams'],
TParams = RouteById<TRouteTree, TFrom>['types']['allParams'],
TSelected = TParams,
>(
opts: TOpts & {
opts: StrictOrFrom<TFrom> & {
select?: (params: TParams) => TSelected
},
): TSelected {
Expand Down
10 changes: 4 additions & 6 deletions packages/react-router/src/useSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { AnyRoute } from './route'
import { AnyRoute, RootSearchSchema } from './route'
import { RouteIds, RouteById } from './routeInfo'
import { RegisteredRouter } from './router'
import { RouteMatch } from './Matches'
import { useMatch } from './Matches'
import { StrictOrFrom, GetTFrom } from './utils'
import { StrictOrFrom } from './utils'

export function useSearch<
TOpts extends StrictOrFrom<TFrom>,
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TFromInferred = GetTFrom<TOpts, TRouteTree>,
TSearch = RouteById<TRouteTree, TFromInferred>['types']['fullSearchSchema'],
TSearch = Exclude<RouteById<TRouteTree, TFrom>['types']['fullSearchSchema'], RootSearchSchema>,
TSelected = TSearch,
>(
opts: TOpts & {
opts: StrictOrFrom<TFrom> & {
select?: (search: TSearch) => TSelected
},
) : TSelected {
Expand Down
6 changes: 0 additions & 6 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,6 @@ export type StrictOrFrom<TFrom> =
strict: false
}

export type GetTFrom<T, TRouteTree extends AnyRoute> = T extends StrictOrFrom<
infer TFrom extends RouteIds<TRouteTree>
>
? TFrom
: never

export function useRouteContext<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
Expand Down

0 comments on commit 46db31b

Please sign in to comment.