Skip to content

Commit

Permalink
chore: fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
chorobin committed Jan 4, 2025
1 parent 6f51802 commit 8a10a13
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 112 deletions.
1 change: 1 addition & 0 deletions e2e/react-router/basic-file-based/src/routes/anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function AnchorComponent() {
{anchors.map((anchor) => (
<li key={anchor.id}>
<Link
from={Route.fullPath}
data-testid={`link-${anchor.id}`}
hash={anchor.id}
activeOptions={{ includeHash: true }}
Expand Down
1 change: 1 addition & 0 deletions examples/react/basic-file-based/src/routes/anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function AnchorComponent() {
{anchors.map((anchor) => (
<li key={anchor.id}>
<Link
from={Route.fullPath}
hash={anchor.id}
activeOptions={{ includeHash: true }}
activeProps={{
Expand Down
33 changes: 16 additions & 17 deletions packages/react-router/src/Matches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ import type {
} from './structuralSharing'
import type { AnyRoute, ReactNode, StaticDataRouteOption } from './route'
import type { AnyRouter, RegisteredRouter, RouterState } from './router'
import type { ResolveRelativePath, ResolveRoute, ToOptions } from './link'
import type {
MakeOptionalPathParams,
MakeOptionalSearchParams,
MaskOptions,
ResolveRelativePath,
ResolveRoute,
ToSubOptionsProps,
} from './link'
import type {
AllContext,
AllLoaderData,
Expand All @@ -23,7 +30,6 @@ import type {
RouteById,
RouteByPath,
RouteIds,
RoutePaths,
} from './routeInfo'
import type {
Constrain,
Expand Down Expand Up @@ -270,22 +276,15 @@ export interface MatchRouteOptions {

export type UseMatchRouteOptions<
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends RoutePaths<TRouter['routeTree']> | string = RoutePaths<
TRouter['routeTree']
>,
TTo extends string | undefined = '',
TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,
TFrom extends string = string,
TTo extends string | undefined = undefined,
TMaskFrom extends string = TFrom,
TMaskTo extends string = '',
TOptions extends ToOptions<
TRouter,
TFrom,
TTo,
TMaskFrom,
TMaskTo
> = ToOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
TRelaxedOptions = Omit<TOptions, 'search' | 'params'> &
DeepPartial<Pick<TOptions, 'search' | 'params'>>,
> = TRelaxedOptions & MatchRouteOptions
> = ToSubOptionsProps<TRouter, TFrom, TTo> &
DeepPartial<MakeOptionalSearchParams<TRouter, TFrom, TTo>> &
DeepPartial<MakeOptionalPathParams<TRouter, TFrom, TTo>> &
MaskOptions<TRouter, TMaskFrom, TMaskTo> &
MatchRouteOptions

export function useMatchRoute<TRouter extends AnyRouter = RegisteredRouter>() {
const router = useRouter()
Expand Down
67 changes: 35 additions & 32 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import type {
RouteByToPath,
RoutePaths,
RouteToPath,
ToPath,
TrailingSlashOptionByRouter,
} from './routeInfo'
import type {
AnyRouter,
Expand Down Expand Up @@ -100,10 +98,8 @@ export type SearchRelativePathAutoComplete<
TTo extends string,
TSearchPath extends string,
> =
| (TSearchPath & RouteToPath<TRouter> extends never
? never
: ToPath<TrailingSlashOptionByRouter<TRouter>, TTo>)
| `${TTo}/${SearchPaths<TRouter, RemoveTrailingSlashes<TSearchPath>>}`
| (TSearchPath & RouteToPath<TRouter> extends never ? never : TTo)
| `${RemoveTrailingSlashes<TTo>}/${SearchPaths<TRouter, RemoveTrailingSlashes<TSearchPath>>}`

export type RelativeToParentPathAutoComplete<
TRouter extends AnyRouter,
Expand All @@ -120,7 +116,7 @@ export type RelativeToParentPathAutoComplete<
RemoveTrailingSlashes<TResolvedPath>
> extends never
? never
: `${TTo}/${ParentPath<TrailingSlashOptionByRouter<TRouter>>}`
: `${RemoveTrailingSlashes<TTo>}/${ParentPath<TRouter>}`
: never)

export type RelativeToCurrentPathAutoComplete<
Expand All @@ -130,22 +126,22 @@ export type RelativeToCurrentPathAutoComplete<
TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
> =
| SearchRelativePathAutoComplete<TRouter, TTo, TResolvedPath>
| CurrentPath<TrailingSlashOptionByRouter<TRouter>>
| CurrentPath<TRouter>

export type AbsolutePathAutoComplete<
TRouter extends AnyRouter,
TFrom extends string,
> =
| (string extends TFrom
? CurrentPath<TrailingSlashOptionByRouter<TRouter>>
? CurrentPath<TRouter>
: TFrom extends `/`
? never
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
: CurrentPath<TRouter>)
| (string extends TFrom
? ParentPath<TrailingSlashOptionByRouter<TRouter>>
? ParentPath<TRouter>
: TFrom extends `/`
? never
: ParentPath<TrailingSlashOptionByRouter<TRouter>>)
: ParentPath<TRouter>)
| RouteToPath<TRouter>
| (TFrom extends '/'
? never
Expand All @@ -164,16 +160,8 @@ export type RelativeToPathAutoComplete<
: TTo & `..${string}` extends never
? TTo & `.${string}` extends never
? AbsolutePathAutoComplete<TRouter, TFrom>
: RelativeToCurrentPathAutoComplete<
TRouter,
TFrom,
RemoveTrailingSlashes<TTo>
>
: RelativeToParentPathAutoComplete<
TRouter,
TFrom,
RemoveTrailingSlashes<TTo>
>
: RelativeToCurrentPathAutoComplete<TRouter, TFrom, TTo>
: RelativeToParentPathAutoComplete<TRouter, TFrom, TTo>

export type NavigateOptions<
TRouter extends AnyRouter = RegisteredRouter,
Expand Down Expand Up @@ -234,19 +222,33 @@ export type ToSubOptions<
SearchParamOptions<TRouter, TFrom, TTo> &
PathParamOptions<TRouter, TFrom, TTo>

export interface RequiredToOptions<
in out TRouter extends AnyRouter,
in out TFrom extends string,
in out TTo extends string | undefined,
> {
to: ToPathOption<TRouter, TFrom, TTo> & {}
}

export interface OptionalToOptions<
in out TRouter extends AnyRouter,
in out TFrom extends string,
in out TTo extends string | undefined,
> {
to?: ToPathOption<TRouter, TFrom, TTo> & {}
}

export type MakeToRequired<
TRouter extends AnyRouter,
TFrom extends string,
TTo extends string | undefined,
> = string extends TFrom
? {
to: ToPathOption<TRouter, TFrom, TTo> & {}
from?: FromPathOption<TRouter, TFrom> & {}
}
: {
to?: ToPathOption<TRouter, TFrom, TTo> & {}
from?: FromPathOption<TRouter, TFrom> & {}
}
? string extends TTo
? OptionalToOptions<TRouter, TFrom, TTo>
: TTo & CatchAllPaths<TRouter> extends never
? RequiredToOptions<TRouter, TFrom, TTo>
: OptionalToOptions<TRouter, TFrom, TTo>
: OptionalToOptions<TRouter, TFrom, TTo>

export type ToSubOptionsProps<
TRouter extends AnyRouter = RegisteredRouter,
Expand All @@ -255,6 +257,7 @@ export type ToSubOptionsProps<
> = MakeToRequired<TRouter, TFrom, TTo> & {
hash?: true | Updater<string>
state?: true | NonNullableUpdater<HistoryState>
from?: FromPathOption<TRouter, TFrom> & {}
}

export type ParamsReducerFn<
Expand Down Expand Up @@ -330,7 +333,7 @@ export type ResolveToParams<
? never
: string extends TPath
? ResolveAllToParams<TRouter, TParamVariant>
: TPath extends CatchAllPaths<TrailingSlashOptionByRouter<TRouter>>
: TPath extends CatchAllPaths<TRouter>
? ResolveAllToParams<TRouter, TParamVariant>
: ResolveRoute<
TRouter,
Expand Down Expand Up @@ -415,7 +418,7 @@ export type IsRequired<
ResolveRelativePath<TFrom, TTo> extends infer TPath
? undefined extends TPath
? never
: TPath extends CatchAllPaths<TrailingSlashOptionByRouter<TRouter>>
: TPath extends CatchAllPaths<TRouter>
? never
: IsRequiredParams<
ResolveRelativeToParams<TRouter, TParamVariant, TFrom, TTo>
Expand Down
36 changes: 17 additions & 19 deletions packages/react-router/src/routeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,23 @@ export type RouteIds<TRouteTree extends AnyRoute> =
? CodeRouteIds<TRouteTree>
: InferFileRouteTypes<TRouteTree>['id']

export type ParentPath<TOption> = 'always' extends TOption
? '../'
: 'never' extends TOption
? '..'
: '../' | '..'

export type CurrentPath<TOption> = 'always' extends TOption
? './'
: 'never' extends TOption
? '.'
: './' | '.'

export type ToPath<TOption, TTo extends string> = 'always' extends TOption
? `${TTo}/`
: 'never' extends TOption
? TTo
: TTo | `${TTo}/`

export type CatchAllPaths<TOption> = CurrentPath<TOption> | ParentPath<TOption>
export type ParentPath<TRouter extends AnyRouter> =
TrailingSlashOptionByRouter<TRouter> extends 'always'
? '../'
: TrailingSlashOptionByRouter<TRouter> extends 'never'
? '..'
: '../' | '..'

export type CurrentPath<TRouter extends AnyRouter> =
TrailingSlashOptionByRouter<TRouter> extends 'always'
? './'
: TrailingSlashOptionByRouter<TRouter> extends 'never'
? '.'
: './' | '.'

export type CatchAllPaths<TRouter extends AnyRouter> =
| CurrentPath<TRouter>
| ParentPath<TRouter>

export type CodeRoutesByPath<TRouteTree extends AnyRoute> =
ParseRoute<TRouteTree> extends infer TRoutes extends AnyRoute
Expand Down
18 changes: 3 additions & 15 deletions packages/react-router/tests/Matches.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,14 @@ test('when matching a route with params', () => {
.parameter(0)
.toHaveProperty('to')
.toEqualTypeOf<
| '/'
| '.'
| '..'
| '/invoices'
| '/invoices/$invoiceId'
| '/comments/$id'
| undefined
'/' | '.' | '..' | '/invoices' | '/invoices/$invoiceId' | '/comments/$id'
>()

expectTypeOf(MatchRoute<DefaultRouter, any, '/invoices/$invoiceId'>)
expectTypeOf(MatchRoute<DefaultRouter, string, '/invoices/$invoiceId'>)
.parameter(0)
.toHaveProperty('to')
.toEqualTypeOf<
| '/'
| '.'
| '..'
| '/invoices'
| '/invoices/$invoiceId'
| '/comments/$id'
| undefined
'/' | '.' | '..' | '/invoices' | '/invoices/$invoiceId' | '/comments/$id'
>()

expectTypeOf(
Expand Down
Loading

0 comments on commit 8a10a13

Please sign in to comment.