Skip to content

Commit

Permalink
fix: allow search routes to define search params that differ in types (
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel authored Dec 20, 2023
1 parent e518865 commit c76e465
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 0 additions & 2 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ export type SearchParamOptions<
TFromSearchEnsured = '/' extends TFrom
? FullSearchSchema<TRouteTree>
: Expand<
UnionToIntersection<
PickRequired<
RouteByPath<TRouteTree, TFrom>['types']['fullSearchSchema']
>
>
>,
TFromSearchOptional = Omit<
FullSearchSchema<TRouteTree>,
Expand Down
18 changes: 16 additions & 2 deletions packages/react-router/src/routeInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyRoute, Route } from './route'
import { Expand, UnionToIntersection } from './utils'
import { Expand, UnionToIntersection, UnionToTuple } from './utils'

export type ParseRoute<TRouteTree extends AnyRoute> =
| TRouteTree
Expand Down Expand Up @@ -59,9 +59,23 @@ export type RoutePaths<TRouteTree extends AnyRoute> =
| ParseRoute<TRouteTree>['fullPath']
| '/'

type UnionizeCollisions<T, U> = {
[P in keyof T & keyof U]: T[P] extends U[P] ? T[P] : T[P] | U[P]
}
type Reducer<T, U, C = UnionizeCollisions<T, U>> = C &
Omit<T, keyof C> &
Omit<U, keyof C>

type Reduce<T extends any[], Result = unknown> = T extends [
infer First,
...infer Rest,
]
? Reduce<Rest, Reducer<Result, First>>
: Result

export type FullSearchSchema<TRouteTree extends AnyRoute> = Partial<
Expand<
UnionToIntersection<ParseRoute<TRouteTree>['types']['fullSearchSchema']>
Reduce<UnionToTuple<ParseRoute<TRouteTree>['types']['fullSearchSchema']>>
>
>

Expand Down
10 changes: 10 additions & 0 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ export type PickExclude<T, U> = {
[K in keyof T as T[K] extends U ? never : K]: T[K]
}

// from https://github.com/type-challenges/type-challenges/issues/737
type LastInUnion<U> = UnionToIntersection<
U extends unknown ? (x: U) => 0 : never
> extends (x: infer L) => 0
? L
: never
export type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never]
? []
: [...UnionToTuple<Exclude<U, Last>>, Last]

//

export const isServer = typeof document === 'undefined'
Expand Down

0 comments on commit c76e465

Please sign in to comment.