Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(react-router): remove root search schema type and unused type parameters from route options #1729

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/react-router/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export {
type AnyRoute,
type RouteConstraints,
type AnyRootRoute,
type RootSearchSchema,
type ResolveFullPath,
type RouteMask,
type ErrorRouteProps,
Expand Down
22 changes: 5 additions & 17 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { deepEqual, functionalUpdate } from './utils'
import { exactPathTest, removeTrailingSlash } from './path'
import type { AnyRouter, ParsedLocation } from '.'
import type { HistoryState } from '@tanstack/history'
import type { AnyRoute, RootSearchSchema } from './route'
import type {
AllParams,
CatchAllPaths,
Expand Down Expand Up @@ -251,9 +250,6 @@ type ParamsReducer<

type ParamVariant = 'PATH' | 'SEARCH'

type ExcludeRootSearchSchema<T> =
Exclude<T, RootSearchSchema> extends never ? {} : Exclude<T, RootSearchSchema>

export type ResolveRoute<
TRouter extends AnyRouter,
TFrom,
Expand All @@ -265,11 +261,6 @@ export type ResolveRoute<
: RouteByToPath<TRouter, TPath>
: never

type PostProcessParams<
T,
TParamVariant extends ParamVariant,
> = TParamVariant extends 'SEARCH' ? ExcludeRootSearchSchema<T> : T

type ResolveFromParamType<TParamVariant extends ParamVariant> =
TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchema'

Expand Down Expand Up @@ -312,14 +303,11 @@ export type ResolveToParams<
? ResolveAllToParams<TRouter, TParamVariant>
: TPath extends CatchAllPaths
? ResolveAllToParams<TRouter, TParamVariant>
: PostProcessParams<
ResolveRoute<
TRouter,
TFrom,
TTo
>['types'][ResolveToParamType<TParamVariant>],
TParamVariant
>
: ResolveRoute<
TRouter,
TFrom,
TTo
>['types'][ResolveToParamType<TParamVariant>]
: never

type ResolveRelativeToParams<
Expand Down
88 changes: 25 additions & 63 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export type RouteOptions<
TPath extends string = string,
TSearchSchemaInput = Record<string, unknown>,
TSearchSchema = {},
TSearchSchemaUsed = {},
TFullSearchSchemaInput = TSearchSchemaUsed,
TFullSearchSchema = TSearchSchema,
TParams = AnyPathParams,
TAllParams = TParams,
Expand All @@ -74,8 +72,6 @@ export type RouteOptions<
TPath,
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TFullSearchSchemaInput,
TFullSearchSchema,
TParams,
TAllParams,
Expand Down Expand Up @@ -170,8 +166,6 @@ export type BaseRouteOptions<
TPath extends string = string,
TSearchSchemaInput = Record<string, unknown>,
TSearchSchema = {},
TSearchSchemaUsed = {},
TFullSearchSchemaInput = TSearchSchemaUsed,
TFullSearchSchema = TSearchSchema,
TParams = {},
TAllParams = ParamsFallback<TPath, TParams>,
Expand Down Expand Up @@ -398,28 +392,15 @@ export type ResolveFullSearchSchema<
TParentRoute extends AnyRoute,
TSearchSchema,
> = unknown extends TParentRoute
? Omit<TSearchSchema, keyof RootSearchSchema>
: Assign<
TParentRoute['id'] extends RootRouteId
? Omit<TParentRoute['types']['searchSchema'], keyof RootSearchSchema>
: TParentRoute['types']['fullSearchSchema'],
TSearchSchema
>
? TSearchSchema
: Assign<TParentRoute['types']['fullSearchSchema'], TSearchSchema>

export type ResolveFullSearchSchemaInput<
TParentRoute extends AnyRoute,
TSearchSchemaUsed,
> = unknown extends TParentRoute
? Omit<TSearchSchemaUsed, keyof RootSearchSchema>
: Assign<
TParentRoute['id'] extends RootRouteId
? Omit<
TParentRoute['types']['searchSchemaInput'],
keyof RootSearchSchema
>
: TParentRoute['types']['fullSearchSchemaInput'],
TSearchSchemaUsed
>
? TSearchSchemaUsed
: Assign<TParentRoute['types']['fullSearchSchemaInput'], TSearchSchemaUsed>

export type ResolveRouteContext<TRouteContextReturn> = [
TRouteContextReturn,
Expand Down Expand Up @@ -628,8 +609,6 @@ export class Route<
TPath,
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TFullSearchSchemaInput,
TFullSearchSchema,
TParams,
TAllParams,
Expand Down Expand Up @@ -667,8 +646,6 @@ export class Route<
TPath,
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TFullSearchSchemaInput,
TFullSearchSchema,
TParams,
TAllParams,
Expand Down Expand Up @@ -723,8 +700,6 @@ export class Route<
TPath,
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TFullSearchSchemaInput,
TFullSearchSchema,
TParams,
TAllParams,
Expand Down Expand Up @@ -969,8 +944,6 @@ export function createRoute<
TPath,
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TFullSearchSchemaInput,
TFullSearchSchema,
TParams,
TAllParams,
Expand Down Expand Up @@ -1010,9 +983,8 @@ export function createRoute<
export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any, any>

export type RootRouteOptions<
TSearchSchemaInput = RootSearchSchema,
TSearchSchema = RootSearchSchema,
TSearchSchemaUsed = RootSearchSchema,
TSearchSchemaInput = {},
TSearchSchema = {},
TRouteContextReturn = RouteContext,
TRouteContext = ResolveRouteContext<TRouteContextReturn>,
TRouterContext = {},
Expand All @@ -1026,8 +998,6 @@ export type RootRouteOptions<
'', // TPath
TSearchSchemaInput, // TSearchSchemaInput
TSearchSchema, // TSearchSchema
TSearchSchemaUsed,
TSearchSchemaUsed, //TFullSearchSchemaInput
TSearchSchema, // TFullSearchSchema
{}, // TParams
{}, // TAllParams
Expand All @@ -1049,21 +1019,22 @@ export type RootRouteOptions<

export function createRootRouteWithContext<TRouterContext extends {}>() {
return <
TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
TSearchSchema extends Record<string, any> = RootSearchSchema,
TSearchSchemaUsed extends Record<string, any> = RootSearchSchema,
TSearchSchemaInput = {},
TSearchSchema = {},
TSearchSchemaUsed = ResolveSearchSchemaUsed<
TSearchSchemaInput,
TSearchSchema
>,
TRouteContextReturn extends RouteContext = RouteContext,
TRouteContext extends RouteContext = [TRouteContextReturn] extends [never]
? RouteContext
: TRouteContextReturn,
TRouteContext extends
RouteContext = ResolveRouteContext<TRouteContextReturn>,
TLoaderDeps extends Record<string, any> = {},
TLoaderDataReturn = {},
TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
>(
options?: RootRouteOptions<
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TRouteContextReturn,
TRouteContext,
TRouterContext,
Expand All @@ -1090,14 +1061,10 @@ export function createRootRouteWithContext<TRouterContext extends {}>() {
*/
export const rootRouteWithContext = createRootRouteWithContext

export type RootSearchSchema = {
__TRootSearchSchema__: '__TRootSearchSchema__'
}

export class RootRoute<
in out TSearchSchemaInput = RootSearchSchema,
in out TSearchSchema = RootSearchSchema,
in out TSearchSchemaUsed = RootSearchSchema,
in out TSearchSchemaInput = {},
in out TSearchSchema = {},
in out TSearchSchemaUsed = {},
TRouteContextReturn = RouteContext,
in out TRouteContext = ResolveRouteContext<TRouteContextReturn>,
in out TRouterContext = {},
Expand Down Expand Up @@ -1133,7 +1100,6 @@ export class RootRoute<
options?: RootRouteOptions<
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TRouteContextReturn,
TRouteContext,
TRouterContext,
Expand All @@ -1147,9 +1113,12 @@ export class RootRoute<
}

export function createRootRoute<
TSearchSchemaInput = RootSearchSchema,
TSearchSchema = RootSearchSchema,
TSearchSchemaUsed = RootSearchSchema,
TSearchSchemaInput = {},
TSearchSchema = {},
TSearchSchemaUsed = ResolveSearchSchemaUsed<
TSearchSchemaInput,
TSearchSchema
>,
TRouteContextReturn = RouteContext,
TRouteContext = ResolveRouteContext<TRouteContextReturn>,
TRouterContext = {},
Expand All @@ -1164,9 +1133,7 @@ export function createRootRoute<
'', // TPath
TSearchSchemaInput, // TSearchSchemaInput
TSearchSchema, // TSearchSchema
TSearchSchemaUsed,
TSearchSchemaUsed, // TFullSearchSchemaInput
TSearchSchema, // TFullSearchSchema
TSearchSchema,
{}, // TParams
{}, // TAllParams
TRouteContextReturn, // TRouteContextReturn
Expand Down Expand Up @@ -1306,10 +1273,7 @@ export class NotFoundRoute<
TFullSearchSchema = ResolveFullSearchSchema<TParentRoute, TSearchSchema>,
TRouteContextReturn = AnyContext,
TRouteContext = RouteContext,
TAllContext = Assign<
IsAny<TParentRoute['types']['allContext'], {}>,
TRouteContext
>,
TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
TRouterContext = AnyContext,
TLoaderDeps extends Record<string, any> = {},
TLoaderDataReturn = {},
Expand Down Expand Up @@ -1345,8 +1309,6 @@ export class NotFoundRoute<
string,
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TFullSearchSchemaInput,
TFullSearchSchema,
{},
{},
Expand Down
9 changes: 3 additions & 6 deletions packages/react-router/src/routeInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyRoute, RootSearchSchema } from './route'
import type { AnyRoute } from './route'
import type { AnyRouter, TrailingSlashOption } from './router'
import type { Expand, MergeUnion } from './utils'

Expand Down Expand Up @@ -115,14 +115,11 @@ export type RouteByToPath<TRouter extends AnyRouter, TTo> = Extract<
>

export type FullSearchSchema<TRouteTree extends AnyRoute> = MergeUnion<
Exclude<ParseRoute<TRouteTree>['types']['fullSearchSchema'], RootSearchSchema>
ParseRoute<TRouteTree>['types']['fullSearchSchema']
>

export type FullSearchSchemaInput<TRouteTree extends AnyRoute> = MergeUnion<
Exclude<
ParseRoute<TRouteTree>['types']['fullSearchSchemaInput'],
RootSearchSchema
>
ParseRoute<TRouteTree>['types']['fullSearchSchemaInput']
>

export type AllParams<TRouteTree extends AnyRoute> = MergeUnion<
Expand Down
9 changes: 3 additions & 6 deletions packages/react-router/src/useSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMatch } from './useMatch'
import type { AnyRoute, RootSearchSchema } from './route'
import type { AnyRoute } from './route'
import type { FullSearchSchema, RouteById, RouteIds } from './routeInfo'
import type { RegisteredRouter } from './router'
import type { MakeRouteMatch } from './Matches'
import type { StrictOrFrom } from './utils'
import type { Expand, StrictOrFrom } from './utils'

export type UseSearchOptions<
TFrom,
Expand All @@ -20,10 +20,7 @@ export function useSearch<
TStrict extends boolean = true,
TSearch = TStrict extends false
? FullSearchSchema<TRouteTree>
: Exclude<
RouteById<TRouteTree, TFrom>['types']['fullSearchSchema'],
RootSearchSchema
>,
: Expand<RouteById<TRouteTree, TFrom>['types']['fullSearchSchema']>,
TSelected = TSearch,
>(opts: UseSearchOptions<TFrom, TStrict, TSearch, TSelected>): TSelected {
return useMatch({
Expand Down
Loading
Loading