Skip to content

Commit

Permalink
fix(react-router): allow union when assigning (#2994)
Browse files Browse the repository at this point in the history
  • Loading branch information
chorobin authored Dec 12, 2024
1 parent a365896 commit ec50a80
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ export type IsUnion<T, U extends T = T> = (
? false
: true

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

export type Timeout = ReturnType<typeof setTimeout>

Expand Down
29 changes: 29 additions & 0 deletions packages/react-router/tests/useSearch.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,32 @@ test('when a route has search params using SearchSchemaInput', () => {
page: number
}>
})

test('when route has a union of search params', () => {
const rootRoute = createRootRoute()

const postRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
validateSearch: (): { status: 'in' } | { status: 'out' } => {
return { status: 'in' }
},
})

const indexRoute = createRoute({
getParentRoute: () => postRoute,
path: '/',
validateSearch: (): { detail: string } => {
return { detail: 'detail' }
},
})

const routeTree = rootRoute.addChildren([
indexRoute.addChildren([indexRoute]),
])
// eslint-disable-next-line unused-imports/no-unused-vars
const router = createRouter({ routeTree })
expectTypeOf(useSearch<typeof router, '/'>).returns.toEqualTypeOf<
{ status: 'in'; detail: string } | { status: 'out'; detail: string }
>
})

0 comments on commit ec50a80

Please sign in to comment.