Skip to content

Commit 1742e3e

Browse files
committed
chore: fix build and further improve types
1 parent 4658afa commit 1742e3e

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

packages/react-router/src/link.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import type {
2222
FullSearchSchema,
2323
FullSearchSchemaInput,
2424
ParentPath,
25-
RootPath,
2625
RouteByPath,
2726
RouteByToPath,
2827
RoutePaths,
2928
RouteToPath,
29+
ToPath,
3030
TrailingSlashOptionByRouter,
3131
} from './routeInfo'
3232
import type {
@@ -79,26 +79,27 @@ export type RemoveLeadingSlashes<T> = T extends `/${string}`
7979
export type FindDescendantPaths<
8080
TRouter extends AnyRouter,
8181
TPrefix extends string,
82-
TCleanedPrefix extends string = RemoveTrailingSlashes<TPrefix>,
83-
> = (TPrefix | `${TCleanedPrefix}/${string}`) & RouteToPath<TRouter>
82+
> = `${TPrefix}/${string}` & RouteToPath<TRouter>
8483

8584
export type SearchPaths<
8685
TRouter extends AnyRouter,
8786
TPrefix extends string,
8887
TPaths = FindDescendantPaths<TRouter, TPrefix>,
89-
TCleanedPrefix extends string = RemoveTrailingSlashes<TPrefix>,
90-
TRootPath = RootPath<TrailingSlashOptionByRouter<TRouter>>,
91-
> = TPaths extends TPrefix
92-
? TRootPath
93-
: TPaths extends `${TCleanedPrefix}/${infer TRest}`
94-
? `/${TRest}`
95-
: never
88+
> = TPaths extends `${TPrefix}/${infer TRest}`
89+
? TRest extends ''
90+
? never
91+
: TRest
92+
: never
9693

9794
export type SearchRelativePathAutoComplete<
9895
TRouter extends AnyRouter,
9996
TTo extends string,
10097
TSearchPath extends string,
101-
> = `${TTo}${SearchPaths<TRouter, TSearchPath>}`
98+
> =
99+
| (TSearchPath & RouteToPath<TRouter> extends never
100+
? never
101+
: ToPath<TrailingSlashOptionByRouter<TRouter>, TTo>)
102+
| `${TTo}/${SearchPaths<TRouter, RemoveTrailingSlashes<TSearchPath>>}`
102103

103104
export type RelativeToParentPathAutoComplete<
104105
TRouter extends AnyRouter,
@@ -110,7 +111,10 @@ export type RelativeToParentPathAutoComplete<
110111
| (TTo extends `${string}..` | `${string}../`
111112
? TResolvedPath extends '/' | ''
112113
? never
113-
: FindDescendantPaths<TRouter, TResolvedPath> extends never
114+
: FindDescendantPaths<
115+
TRouter,
116+
RemoveTrailingSlashes<TResolvedPath>
117+
> extends never
114118
? never
115119
: `${TTo}/${ParentPath<TrailingSlashOptionByRouter<TRouter>>}`
116120
: never)
@@ -132,9 +136,7 @@ export type AbsolutePathAutoComplete<
132136
? CurrentPath<TrailingSlashOptionByRouter<TRouter>>
133137
: TFrom extends `/`
134138
? never
135-
: FindDescendantPaths<TRouter, TFrom> extends never
136-
? never
137-
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
139+
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
138140
| (string extends TFrom
139141
? ParentPath<TrailingSlashOptionByRouter<TRouter>>
140142
: TFrom extends `/`
@@ -145,9 +147,7 @@ export type AbsolutePathAutoComplete<
145147
? never
146148
: string extends TFrom
147149
? never
148-
: RemoveLeadingSlashes<
149-
SearchPaths<TRouter, RemoveTrailingSlashes<TFrom>>
150-
>)
150+
: SearchPaths<TRouter, RemoveTrailingSlashes<TFrom>>)
151151

152152
export type RelativeToPathAutoComplete<
153153
TRouter extends AnyRouter,

packages/react-router/src/routeInfo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ export type CurrentPath<TOption> = 'always' extends TOption
7171
? '.'
7272
: './' | '.'
7373

74-
export type RootPath<TOption> = 'always' extends TOption
75-
? '/'
74+
export type ToPath<TOption, TTo extends string> = 'always' extends TOption
75+
? `${TTo}/`
7676
: 'never' extends TOption
77-
? ''
78-
: '' | '/'
77+
? TTo
78+
: TTo | `${TTo}/`
7979

8080
export type CatchAllPaths<TOption> = CurrentPath<TOption> | ParentPath<TOption>
8181

packages/react-router/tests/link.test-d.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ test('when navigating from a route with no params and no search to the root', ()
396396
.parameter(0)
397397
.toHaveProperty('to')
398398
.toEqualTypeOf<
399-
| ''
400399
| '..'
401400
| '.'
402401
| '/'
@@ -416,7 +415,6 @@ test('when navigating from a route with no params and no search to the root', ()
416415
.parameter(0)
417416
.toHaveProperty('to')
418417
.toEqualTypeOf<
419-
| ''
420418
| '..'
421419
| '.'
422420
| '/'
@@ -439,7 +437,6 @@ test('when navigating from a route with no params and no search to the root', ()
439437
| '../'
440438
| './'
441439
| '/'
442-
| ''
443440
| '/invoices/'
444441
| '/invoices/$invoiceId/'
445442
| '/invoices/$invoiceId/edit/'
@@ -456,7 +453,6 @@ test('when navigating from a route with no params and no search to the root', ()
456453
.parameter(0)
457454
.toHaveProperty('to')
458455
.toEqualTypeOf<
459-
| ''
460456
| '..'
461457
| '.'
462458
| '/'
@@ -479,10 +475,8 @@ test('when navigating from a route with no params and no search to the root', ()
479475
| '.'
480476
| '..'
481477
| '../'
482-
| '../'
483478
| './'
484479
| '/'
485-
| ''
486480
| '/invoices'
487481
| '/invoices/'
488482
| '/invoices/$invoiceId'

packages/react-router/tests/route.test-d.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,6 @@ test('when creating a child route with context, search, params and beforeLoad',
12451245
.toHaveProperty('to')
12461246
.toEqualTypeOf<
12471247
| '.'
1248-
| './'
12491248
| './invoices'
12501249
| './invoices/$invoiceId'
12511250
| './invoices/$invoiceId/details'

0 commit comments

Comments
 (0)