Skip to content

feat: add support for shouldThrow in RouteApi #3642

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions packages/react-router/src/fileRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,21 @@ export class LazyRoute<TRoute extends AnyCoreRoute> {
}

useSearch: UseSearchRoute<TRoute['id']> = (opts) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return useSearch({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
shouldThrow: opts?.shouldThrow,
from: this.options.id,
} as any) as any
}

useParams: UseParamsRoute<TRoute['id']> = (opts) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return useParams({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
shouldThrow: opts?.shouldThrow,
from: this.options.id,
} as any) as any
} as any)
}

useLoaderDeps: UseLoaderDepsRoute<TRoute['id']> = (opts) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,21 @@ export class RouteApi<TId, TRouter extends AnyRouter = RegisteredRouter> {
}

useSearch: UseSearchRoute<TId> = (opts) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return useSearch({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
shouldThrow: opts?.shouldThrow,
from: this.id,
} as any) as any
}

useParams: UseParamsRoute<TId> = (opts) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return useParams({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
shouldThrow: opts?.shouldThrow,
from: this.id,
} as any) as any
} as any)
}

useLoaderDeps: UseLoaderDepsRoute<TId> = (opts) => {
Expand Down
8 changes: 6 additions & 2 deletions packages/react-router/src/useParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ export type UseParamsRoute<out TFrom> = <
TRouter extends AnyRouter = RegisteredRouter,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
TThrow extends boolean = true,
>(
opts?: UseParamsBaseOptions<
TRouter,
TFrom,
/* TStrict */ true,
/* TThrow */ true,
TThrow,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
) => UseParamsResult<TRouter, TFrom, true, TSelected>
) => ThrowOrOptional<
UseParamsResult<TRouter, TFrom, /* TStrict */ true, TSelected>,
TThrow
>

export function useParams<
TRouter extends AnyRouter = RegisteredRouter,
Expand Down
5 changes: 3 additions & 2 deletions packages/react-router/src/useSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ export type UseSearchRoute<out TFrom> = <
TRouter extends AnyRouter = RegisteredRouter,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
TThrow extends boolean = true,
>(
opts?: UseSearchBaseOptions<
TRouter,
TFrom,
/* TStrict */ true,
/* TThrow */ true,
TThrow,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
) => UseSearchResult<TRouter, TFrom, true, TSelected>
) => ThrowOrOptional<UseSearchResult<TRouter, TFrom, true, TSelected>, TThrow>

export function useSearch<
TRouter extends AnyRouter = RegisteredRouter,
Expand Down
39 changes: 39 additions & 0 deletions packages/react-router/tests/routeApi.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,26 @@ describe('getRouteApi', () => {
expectTypeOf(invoiceRouteApi.useParams<DefaultRouter>()).toEqualTypeOf<{
invoiceId: string
}>()

expectTypeOf(
invoiceRouteApi.useParams<
DefaultRouter,
/* TSelected */ unknown,
/* TStructuralSharing */ boolean,
/* TThrow */ true
>({ shouldThrow: true }),
).toEqualTypeOf<{ invoiceId: string }>()

expectTypeOf(
invoiceRouteApi.useParams<
DefaultRouter,
/* TSelected */ unknown,
/* TStructuralSharing */ boolean,
/* TThrow */ false
>({ shouldThrow: false }),
).toEqualTypeOf<{ invoiceId: string } | undefined>()
})

test('useContext', () => {
expectTypeOf(
invoiceRouteApi.useRouteContext<DefaultRouter>(),
Expand All @@ -71,6 +90,26 @@ describe('getRouteApi', () => {
expectTypeOf(invoiceRouteApi.useSearch<DefaultRouter>()).toEqualTypeOf<{
page: number
}>()

expectTypeOf(
invoiceRouteApi.useSearch<
DefaultRouter,
/* TSelected */ unknown,
/* TStructuralSharing */ boolean,
/* TThrow */ true
>({ shouldThrow: true }),
).toEqualTypeOf<{
page: number
}>()

expectTypeOf(
invoiceRouteApi.useSearch<
DefaultRouter,
/* TSelected */ unknown,
/* TStructuralSharing */ boolean,
/* TThrow */ false
>({ shouldThrow: false }),
).toEqualTypeOf<{ page: number } | undefined>()
})
test('useLoaderData', () => {
expectTypeOf(invoiceRouteApi.useLoaderData<DefaultRouter>()).toEqualTypeOf<{
Expand Down
Loading