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

fix(react-router): useMatch returns union of matches when strict = false #3013

Merged
merged 1 commit into from
Dec 14, 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
13 changes: 10 additions & 3 deletions packages/react-router/src/useMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
ValidateSelected,
} from './structuralSharing'
import type { AnyRouter, RegisteredRouter } from './router'
import type { MakeRouteMatch } from './Matches'
import type { MakeRouteMatch, MakeRouteMatchUnion } from './Matches'
import type { StrictOrFrom, ThrowOrOptional } from './utils'

export interface UseMatchBaseOptions<
Expand Down Expand Up @@ -64,9 +64,16 @@ export type UseMatchResult<
TStrict extends boolean,
TSelected,
> = unknown extends TSelected
? MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>
? TStrict extends true
? MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>
: MakeRouteMatchUnion<TRouter>
: TSelected

type ThrowConstraint<
TStrict extends boolean,
TThrow extends boolean,
> = TStrict extends false ? (TThrow extends true ? never : TThrow) : TThrow

export function useMatch<
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends string | undefined = undefined,
Expand All @@ -80,7 +87,7 @@ export function useMatch<
TFrom,
TStrict,
TSelected,
TThrow,
ThrowConstraint<TStrict, TThrow>,
TStructuralSharing
>,
): ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow> {
Expand Down
70 changes: 48 additions & 22 deletions packages/react-router/tests/useMatch.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expectTypeOf, test } from 'vitest'
import { createRootRoute, createRoute, createRouter, useMatch } from '../src'
import type { MakeRouteMatch } from '../src/Matches'
import type { MakeRouteMatch, MakeRouteMatchUnion } from '../src/Matches'

const rootRoute = createRootRoute()

Expand All @@ -24,30 +24,56 @@ type DefaultRouter = typeof defaultRouter
type TRouteMatch = MakeRouteMatch<DefaultRouter['routeTree']>

describe('useMatch', () => {
const from = '/invoices'
test('return type is `RouteMatch` when shouldThrow = true', () => {
const shouldThrow = true
const match = useMatch<
DefaultRouter,
typeof from,
true, // TStrict
typeof shouldThrow,
TRouteMatch
>({ from, shouldThrow })
describe('shouldThrow', () => {
const from = '/invoices'
test('return type is `RouteMatch` when shouldThrow = true', () => {
const shouldThrow = true
const match = useMatch<
DefaultRouter,
typeof from,
true, // TStrict
typeof shouldThrow,
TRouteMatch
>({ from, shouldThrow })

expectTypeOf(match).toEqualTypeOf<TRouteMatch>()
})

expectTypeOf(match).toEqualTypeOf<TRouteMatch>()
test('return type is `RouteMatch | undefined` when shouldThrow = false', () => {
const shouldThrow = false
const match = useMatch<
DefaultRouter,
typeof from,
true, // TStrict
typeof shouldThrow,
TRouteMatch
>({ from, shouldThrow })

expectTypeOf(match).toEqualTypeOf<TRouteMatch | undefined>()
})
})

test('return type is `RouteMatch | undefined` when shouldThrow = false', () => {
const shouldThrow = false
const match = useMatch<
DefaultRouter,
typeof from,
true, // TStrict
typeof shouldThrow,
TRouteMatch
>({ from, shouldThrow })
test('return type is union of matches when strict = false', () => {
const strict = false as const
const match = useMatch<DefaultRouter, typeof undefined, typeof strict>({
strict,
})

expectTypeOf(match).toEqualTypeOf<TRouteMatch | undefined>()
expectTypeOf(match).toEqualTypeOf<MakeRouteMatchUnion<DefaultRouter>>()
})

test('shouldThrow must be false when strict is false', () => {
const strict = false as const
const shouldThrow = true as const
useMatch<
DefaultRouter,
typeof undefined,
typeof strict,
typeof shouldThrow
>({
strict,
// @ts-expect-error shouldThrow must be false when strict is false
shouldThrow,
})
})
})
Loading