From 46db31bad7b5bfcc5490518eb4a6f46b949bd62c Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Wed, 17 Jan 2024 17:42:22 +0100 Subject: [PATCH] fix: omit `RootSearchSchema` from search param type (#1010) --- packages/react-router/src/Matches.tsx | 31 +++++++++++-------------- packages/react-router/src/useParams.tsx | 10 ++++---- packages/react-router/src/useSearch.tsx | 10 ++++---- packages/react-router/src/utils.ts | 6 ----- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/packages/react-router/src/Matches.tsx b/packages/react-router/src/Matches.tsx index ef7d793ac7..27831a1ff5 100644 --- a/packages/react-router/src/Matches.tsx +++ b/packages/react-router/src/Matches.tsx @@ -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, @@ -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(undefined) @@ -36,7 +36,10 @@ export interface RouteMatch< loaderData?: RouteById['types']['loaderData'] routeContext: RouteById['types']['routeContext'] context: RouteById['types']['allContext'] - search: RouteById['types']['fullSearchSchema'] + search: Exclude< + RouteById['types']['fullSearchSchema'], + RootSearchSchema + > fetchCount: number abortController: AbortController cause: 'preload' | 'enter' | 'stay' @@ -291,14 +294,12 @@ export function getRenderedMatches(state: RouterState) { } export function useMatch< - TOpts extends StrictOrFrom, TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds = RouteIds, - TFromInferred extends RouteIds = GetTFrom, - TRouteMatchState = RouteMatch, + TRouteMatchState = RouteMatch, TSelected = TRouteMatchState, >( - opts: TOpts & { + opts: StrictOrFrom & { select?: (match: TRouteMatchState) => TSelected }, ): TSelected { @@ -377,17 +378,15 @@ export function useParentMatches(opts?: { } export function useLoaderDeps< - TOpts extends StrictOrFrom, TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds = RouteIds, - TFromInferred extends RouteIds = GetTFrom, - TRouteMatch extends RouteMatch = RouteMatch< + TRouteMatch extends RouteMatch = RouteMatch< TRouteTree, - TFromInferred + TFrom >, TSelected = Required['loaderDeps'], >( - opts: TOpts & { + opts: StrictOrFrom & { select?: (match: TRouteMatch) => TSelected }, ): TSelected { @@ -402,17 +401,15 @@ export function useLoaderDeps< } export function useLoaderData< - TOpts extends StrictOrFrom, TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds = RouteIds, - TFromInferred extends RouteIds = GetTFrom, - TRouteMatch extends RouteMatch = RouteMatch< + TRouteMatch extends RouteMatch = RouteMatch< TRouteTree, - TFromInferred + TFrom >, TSelected = Required['loaderData'], >( - opts: TOpts & { + opts: StrictOrFrom & { select?: (match: TRouteMatch) => TSelected }, ): TSelected { diff --git a/packages/react-router/src/useParams.tsx b/packages/react-router/src/useParams.tsx index 6231c7abb0..8a5428f7b6 100644 --- a/packages/react-router/src/useParams.tsx +++ b/packages/react-router/src/useParams.tsx @@ -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, TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds = RouteIds, - TFromInferred = GetTFrom, - TParams = RouteById['types']['allParams'], + TParams = RouteById['types']['allParams'], TSelected = TParams, >( - opts: TOpts & { + opts: StrictOrFrom & { select?: (params: TParams) => TSelected }, ): TSelected { diff --git a/packages/react-router/src/useSearch.tsx b/packages/react-router/src/useSearch.tsx index 10dc5c7bee..2096a87853 100644 --- a/packages/react-router/src/useSearch.tsx +++ b/packages/react-router/src/useSearch.tsx @@ -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, TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds = RouteIds, - TFromInferred = GetTFrom, - TSearch = RouteById['types']['fullSearchSchema'], + TSearch = Exclude['types']['fullSearchSchema'], RootSearchSchema>, TSelected = TSearch, >( - opts: TOpts & { + opts: StrictOrFrom & { select?: (search: TSearch) => TSelected }, ) : TSelected { diff --git a/packages/react-router/src/utils.ts b/packages/react-router/src/utils.ts index 566c5a58f9..d415cca674 100644 --- a/packages/react-router/src/utils.ts +++ b/packages/react-router/src/utils.ts @@ -306,12 +306,6 @@ export type StrictOrFrom = strict: false } -export type GetTFrom = T extends StrictOrFrom< - infer TFrom extends RouteIds -> - ? TFrom - : never - export function useRouteContext< TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds = RouteIds,