diff --git a/packages/react-router/src/link.tsx b/packages/react-router/src/link.tsx index 90b864c90a..a816d14006 100644 --- a/packages/react-router/src/link.tsx +++ b/packages/react-router/src/link.tsx @@ -22,11 +22,11 @@ import type { FullSearchSchema, FullSearchSchemaInput, ParentPath, - RootPath, RouteByPath, RouteByToPath, RoutePaths, RouteToPath, + ToPath, TrailingSlashOptionByRouter, } from './routeInfo' import type { @@ -79,26 +79,27 @@ export type RemoveLeadingSlashes = T extends `/${string}` export type FindDescendantPaths< TRouter extends AnyRouter, TPrefix extends string, - TCleanedPrefix extends string = RemoveTrailingSlashes, -> = (TPrefix | `${TCleanedPrefix}/${string}`) & RouteToPath +> = `${TPrefix}/${string}` & RouteToPath export type SearchPaths< TRouter extends AnyRouter, TPrefix extends string, TPaths = FindDescendantPaths, - TCleanedPrefix extends string = RemoveTrailingSlashes, - TRootPath = RootPath>, -> = 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}` +> = + | (TSearchPath & RouteToPath extends never + ? never + : ToPath, TTo>) + | `${TTo}/${SearchPaths>}` export type RelativeToParentPathAutoComplete< TRouter extends AnyRouter, @@ -110,7 +111,10 @@ export type RelativeToParentPathAutoComplete< | (TTo extends `${string}..` | `${string}../` ? TResolvedPath extends '/' | '' ? never - : FindDescendantPaths extends never + : FindDescendantPaths< + TRouter, + RemoveTrailingSlashes + > extends never ? never : `${TTo}/${ParentPath>}` : never) @@ -132,9 +136,7 @@ export type AbsolutePathAutoComplete< ? CurrentPath> : TFrom extends `/` ? never - : FindDescendantPaths extends never - ? never - : CurrentPath>) + : CurrentPath>) | (string extends TFrom ? ParentPath> : TFrom extends `/` @@ -145,9 +147,7 @@ export type AbsolutePathAutoComplete< ? never : string extends TFrom ? never - : RemoveLeadingSlashes< - SearchPaths> - >) + : SearchPaths>) export type RelativeToPathAutoComplete< TRouter extends AnyRouter, diff --git a/packages/react-router/src/routeInfo.ts b/packages/react-router/src/routeInfo.ts index 52863c0044..bcb74c9cfd 100644 --- a/packages/react-router/src/routeInfo.ts +++ b/packages/react-router/src/routeInfo.ts @@ -71,11 +71,11 @@ export type CurrentPath = 'always' extends TOption ? '.' : './' | '.' -export type RootPath = 'always' extends TOption - ? '/' +export type ToPath = 'always' extends TOption + ? `${TTo}/` : 'never' extends TOption - ? '' - : '' | '/' + ? TTo + : TTo | `${TTo}/` export type CatchAllPaths = CurrentPath | ParentPath diff --git a/packages/react-router/tests/link.test-d.tsx b/packages/react-router/tests/link.test-d.tsx index 436ea4c593..19903adbe4 100644 --- a/packages/react-router/tests/link.test-d.tsx +++ b/packages/react-router/tests/link.test-d.tsx @@ -396,7 +396,6 @@ test('when navigating from a route with no params and no search to the root', () .parameter(0) .toHaveProperty('to') .toEqualTypeOf< - | '' | '..' | '.' | '/' @@ -416,7 +415,6 @@ test('when navigating from a route with no params and no search to the root', () .parameter(0) .toHaveProperty('to') .toEqualTypeOf< - | '' | '..' | '.' | '/' @@ -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/' @@ -456,7 +453,6 @@ test('when navigating from a route with no params and no search to the root', () .parameter(0) .toHaveProperty('to') .toEqualTypeOf< - | '' | '..' | '.' | '/' @@ -479,10 +475,8 @@ test('when navigating from a route with no params and no search to the root', () | '.' | '..' | '../' - | '../' | './' | '/' - | '' | '/invoices' | '/invoices/' | '/invoices/$invoiceId' diff --git a/packages/react-router/tests/route.test-d.tsx b/packages/react-router/tests/route.test-d.tsx index f54a0cb7a9..fd7311de6f 100644 --- a/packages/react-router/tests/route.test-d.tsx +++ b/packages/react-router/tests/route.test-d.tsx @@ -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'