Skip to content

Commit

Permalink
fix: type safe search params for index route (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel authored Jan 8, 2024
1 parent a2c845b commit 2cedaa8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
17 changes: 9 additions & 8 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { useMatch } from './Matches'
import { useRouter, useRouterState } from './RouterProvider'
import { Trim } from './fileRoute'
import { AnyRoute, ReactNode } from './route'
import { AnyRoute, ReactNode, RootSearchSchema } from './route'
import {
AllParams,
FullSearchSchema,
Expand Down Expand Up @@ -171,7 +171,7 @@ type ParamVariant = 'PATH' | 'SEARCH'
export type ParamOptions<
TRouteTree extends AnyRoute,
TFrom,
TTo,
TTo extends string,
TResolved,
TParamVariant extends ParamVariant,
TFromRouteType extends
Expand All @@ -184,12 +184,13 @@ export type ParamOptions<
| 'fullSearchSchemaInput' = TParamVariant extends 'PATH'
? 'allParams'
: 'fullSearchSchemaInput',
TFromParams = Expand<RouteByPath<TRouteTree, TFrom>['types'][TFromRouteType]>,
TToParams = TTo extends ''
TFromParams = Expand<Exclude<RouteByPath<TRouteTree, TFrom>['types'][TFromRouteType], RootSearchSchema>>,
TToIndex = RouteByPath<TRouteTree, `${TTo}/`> extends never ? TTo : `${TTo}/`,
TToParams = TToIndex extends ''
? TFromParams
: never extends TResolved
? Expand<RouteByPath<TRouteTree, TTo>['types'][TToRouteType]>
: Expand<RouteByPath<TRouteTree, TResolved>['types'][TToRouteType]>,
? Expand<Exclude<RouteByPath<TRouteTree, TToIndex>['types'][TToRouteType], RootSearchSchema>>
: Expand<Exclude<RouteByPath<TRouteTree, TResolved>['types'][TToRouteType], RootSearchSchema>>,
TReducer = ParamsReducer<TFromParams, TToParams>,
> = Expand<WithoutEmpty<PickRequired<TToParams>>> extends never
? Partial<MakeParamOption<TParamVariant, true | TReducer>>
Expand All @@ -209,14 +210,14 @@ type MakePathParamOptions<T> = { params: T }
export type SearchParamOptions<
TRouteTree extends AnyRoute,
TFrom,
TTo,
TTo extends string,
TResolved,
> = ParamOptions<TRouteTree, TFrom, TTo, TResolved, 'SEARCH'>

export type PathParamOptions<
TRouteTree extends AnyRoute,
TFrom,
TTo,
TTo extends string,
TResolved,
> = ParamOptions<TRouteTree, TFrom, TTo, TResolved, 'PATH'>

Expand Down
28 changes: 20 additions & 8 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,19 @@ export type InferFullSearchSchemaInput<TRoute> = TRoute extends {
: {}

export type ResolveFullSearchSchema<TParentRoute, TSearchSchema> = Expand<
Assign<InferFullSearchSchema<TParentRoute>, TSearchSchema>
Assign<
Omit<InferFullSearchSchema<TParentRoute>, keyof RootSearchSchema>,
TSearchSchema
>
>

export type ResolveFullSearchSchemaInput<TParentRoute, TSearchSchemaUsed> =
Expand<Assign<InferFullSearchSchemaInput<TParentRoute>, TSearchSchemaUsed>>
Expand<
Assign<
Omit<InferFullSearchSchemaInput<TParentRoute>, keyof RootSearchSchema>,
TSearchSchemaUsed
>
>

export interface AnyRoute
extends Route<
Expand Down Expand Up @@ -742,9 +750,9 @@ export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any>

export function rootRouteWithContext<TRouterContext extends {}>() {
return <
TSearchSchemaInput extends Record<string, any> = {},
TSearchSchema extends Record<string, any> = {},
TSearchSchemaUsed extends Record<string, any> = {},
TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
TSearchSchema extends Record<string, any> = RootSearchSchema,
TSearchSchemaUsed extends Record<string, any> = RootSearchSchema,
TRouteContext extends RouteContext = RouteContext,
TLoaderDeps extends Record<string, any> = {},
TLoaderData extends any = unknown,
Expand Down Expand Up @@ -784,10 +792,14 @@ export function rootRouteWithContext<TRouterContext extends {}>() {
}
}

export type RootSearchSchema = {
__TRootSearchSchema__: '__TRootSearchSchema__'
}

export class RootRoute<
TSearchSchemaInput extends Record<string, any> = {},
TSearchSchema extends Record<string, any> = {},
TSearchSchemaUsed extends Record<string, any> = {},
TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
TSearchSchema extends Record<string, any> = RootSearchSchema,
TSearchSchemaUsed extends Record<string, any> = RootSearchSchema,
TRouteContext extends RouteContext = RouteContext,
TRouterContext extends {} = {},
TLoaderDeps extends Record<string, any> = {},
Expand Down

0 comments on commit 2cedaa8

Please sign in to comment.