Skip to content

Commit

Permalink
chore: fix build and further improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
chorobin committed Dec 29, 2024
1 parent 4658afa commit 1742e3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
36 changes: 18 additions & 18 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import type {
FullSearchSchema,
FullSearchSchemaInput,
ParentPath,
RootPath,
RouteByPath,
RouteByToPath,
RoutePaths,
RouteToPath,
ToPath,
TrailingSlashOptionByRouter,
} from './routeInfo'
import type {
Expand Down Expand Up @@ -79,26 +79,27 @@ export type RemoveLeadingSlashes<T> = T extends `/${string}`
export type FindDescendantPaths<
TRouter extends AnyRouter,
TPrefix extends string,
TCleanedPrefix extends string = RemoveTrailingSlashes<TPrefix>,
> = (TPrefix | `${TCleanedPrefix}/${string}`) & RouteToPath<TRouter>
> = `${TPrefix}/${string}` & RouteToPath<TRouter>

export type SearchPaths<
TRouter extends AnyRouter,
TPrefix extends string,
TPaths = FindDescendantPaths<TRouter, TPrefix>,
TCleanedPrefix extends string = RemoveTrailingSlashes<TPrefix>,
TRootPath = RootPath<TrailingSlashOptionByRouter<TRouter>>,
> = TPaths extends TPrefix
? TRootPath
: TPaths extends `${TCleanedPrefix}/${infer TRest}`
? `/${TRest}`
: never
> = TPaths extends `${TPrefix}/${infer TRest}`
? TRest extends ''
? never
: TRest
: never

export type SearchRelativePathAutoComplete<
TRouter extends AnyRouter,
TTo extends string,
TSearchPath extends string,
> = `${TTo}${SearchPaths<TRouter, TSearchPath>}`
> =
| (TSearchPath & RouteToPath<TRouter> extends never
? never
: ToPath<TrailingSlashOptionByRouter<TRouter>, TTo>)
| `${TTo}/${SearchPaths<TRouter, RemoveTrailingSlashes<TSearchPath>>}`

export type RelativeToParentPathAutoComplete<
TRouter extends AnyRouter,
Expand All @@ -110,7 +111,10 @@ export type RelativeToParentPathAutoComplete<
| (TTo extends `${string}..` | `${string}../`
? TResolvedPath extends '/' | ''
? never
: FindDescendantPaths<TRouter, TResolvedPath> extends never
: FindDescendantPaths<
TRouter,
RemoveTrailingSlashes<TResolvedPath>
> extends never
? never
: `${TTo}/${ParentPath<TrailingSlashOptionByRouter<TRouter>>}`
: never)
Expand All @@ -132,9 +136,7 @@ export type AbsolutePathAutoComplete<
? CurrentPath<TrailingSlashOptionByRouter<TRouter>>
: TFrom extends `/`
? never
: FindDescendantPaths<TRouter, TFrom> extends never
? never
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
| (string extends TFrom
? ParentPath<TrailingSlashOptionByRouter<TRouter>>
: TFrom extends `/`
Expand All @@ -145,9 +147,7 @@ export type AbsolutePathAutoComplete<
? never
: string extends TFrom
? never
: RemoveLeadingSlashes<
SearchPaths<TRouter, RemoveTrailingSlashes<TFrom>>
>)
: SearchPaths<TRouter, RemoveTrailingSlashes<TFrom>>)

export type RelativeToPathAutoComplete<
TRouter extends AnyRouter,
Expand Down
8 changes: 4 additions & 4 deletions packages/react-router/src/routeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export type CurrentPath<TOption> = 'always' extends TOption
? '.'
: './' | '.'

export type RootPath<TOption> = 'always' 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>

Expand Down
6 changes: 0 additions & 6 deletions packages/react-router/tests/link.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ test('when navigating from a route with no params and no search to the root', ()
.parameter(0)
.toHaveProperty('to')
.toEqualTypeOf<
| ''
| '..'
| '.'
| '/'
Expand All @@ -416,7 +415,6 @@ test('when navigating from a route with no params and no search to the root', ()
.parameter(0)
.toHaveProperty('to')
.toEqualTypeOf<
| ''
| '..'
| '.'
| '/'
Expand All @@ -439,7 +437,6 @@ test('when navigating from a route with no params and no search to the root', ()
| '../'
| './'
| '/'
| ''
| '/invoices/'
| '/invoices/$invoiceId/'
| '/invoices/$invoiceId/edit/'
Expand All @@ -456,7 +453,6 @@ test('when navigating from a route with no params and no search to the root', ()
.parameter(0)
.toHaveProperty('to')
.toEqualTypeOf<
| ''
| '..'
| '.'
| '/'
Expand All @@ -479,10 +475,8 @@ test('when navigating from a route with no params and no search to the root', ()
| '.'
| '..'
| '../'
| '../'
| './'
| '/'
| ''
| '/invoices'
| '/invoices/'
| '/invoices/$invoiceId'
Expand Down
1 change: 0 additions & 1 deletion packages/react-router/tests/route.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,6 @@ test('when creating a child route with context, search, params and beforeLoad',
.toHaveProperty('to')
.toEqualTypeOf<
| '.'
| './'
| './invoices'
| './invoices/$invoiceId'
| './invoices/$invoiceId/details'
Expand Down

0 comments on commit 1742e3e

Please sign in to comment.