Skip to content

Commit

Permalink
fix: correct typing for Route.useNavigate (#1940)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Jul 24, 2024
1 parent 789c466 commit b212862
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ export class Route<
return useLoaderData({ ...opts, from: this.id } as any)
}

useNavigate = (): UseNavigateResult<string> => {
useNavigate = (): UseNavigateResult<TFullPath> => {
return useNavigate({ from: this.id })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,31 @@ const defaultRouter = createRouter({

type DefaultRouter = typeof defaultRouter

describe('createRouteApi', () => {
type ExtractDefaultFrom<T> =
T extends UseNavigateResult<infer DefaultFrom> ? DefaultFrom : never

describe('getRouteApi', () => {
const invoiceRouteApi = getRouteApi<'/invoices/$invoiceId', DefaultRouter>(
'/invoices/$invoiceId',
)
describe('useNavigate', () => {
test('has a static `from`', () => {
const navigate = invoiceRouteApi.useNavigate()
navigate
expectTypeOf<
ExtractDefaultFrom<typeof navigate>
>().toEqualTypeOf<'/invoices/$invoiceId'>()
})
})
})

expectTypeOf(navigate).toEqualTypeOf<
UseNavigateResult<'/invoices/$invoiceId'>
>()
describe('createRoute', () => {
describe('useNavigate', () => {
test('has a static `from`', () => {
const navigate = invoiceRoute.useNavigate()
expectTypeOf<
ExtractDefaultFrom<typeof navigate>
>().toEqualTypeOf<'/invoices/$invoiceId'>()
})
})
})

0 comments on commit b212862

Please sign in to comment.