Skip to content

Commit

Permalink
fix(react-router): use intersections for search param merging (#3220)
Browse files Browse the repository at this point in the history
* fix(react-router): use intersections for search param merging

* chore: add type tests and refactor

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
chorobin and autofix-ci[bot] authored Jan 25, 2025
1 parent a0e8481 commit 5b0a82b
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 94 deletions.
1 change: 1 addition & 0 deletions packages/react-router/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ export type {
Expand,
MergeAll,
Assign,
IntersectAssign,
} from './utils'

export type {
Expand Down
5 changes: 3 additions & 2 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
Constrain,
ConstrainLiteral,
Expand,
IntersectAssign,
NoInfer,
} from './utils'
import type { BuildLocationFn, NavigateFn } from './RouterProvider'
Expand Down Expand Up @@ -676,15 +677,15 @@ export type ResolveFullSearchSchema<
TSearchValidator,
> = unknown extends TParentRoute
? ResolveValidatorOutput<TSearchValidator>
: Assign<
: IntersectAssign<
InferFullSearchSchema<TParentRoute>,
ResolveValidatorOutput<TSearchValidator>
>

export type ResolveFullSearchSchemaInput<
TParentRoute extends AnyRoute,
TSearchValidator,
> = Assign<
> = IntersectAssign<
InferFullSearchSchemaInput<TParentRoute>,
ResolveSearchValidatorInput<TSearchValidator>
>
Expand Down
20 changes: 18 additions & 2 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,34 @@ export type IsUnion<T, U extends T = T> = (
? false
: true

export type IsNonEmptyObject<T> = T extends object
? keyof T extends never
? false
: true
: false

export type Assign<TLeft, TRight> = TLeft extends any
? TRight extends any
? keyof TLeft extends never
? IsNonEmptyObject<TLeft> extends false
? TRight
: keyof TRight extends never
: IsNonEmptyObject<TRight> extends false
? TLeft
: keyof TLeft & keyof TRight extends never
? TLeft & TRight
: Omit<TLeft, keyof TRight> & TRight
: never
: never

export type IntersectAssign<TLeft, TRight> = TLeft extends any
? TRight extends any
? IsNonEmptyObject<TLeft> extends false
? TRight
: IsNonEmptyObject<TRight> extends false
? TLeft
: TRight & TLeft
: never
: never

export type Timeout = ReturnType<typeof setTimeout>

export type Updater<TPrevious, TResult = TPrevious> =
Expand Down
Loading

0 comments on commit 5b0a82b

Please sign in to comment.