diff --git a/packages/react-router/src/Matches.tsx b/packages/react-router/src/Matches.tsx index f827e338312..27e9b376413 100644 --- a/packages/react-router/src/Matches.tsx +++ b/packages/react-router/src/Matches.tsx @@ -13,30 +13,26 @@ import type { } from './structuralSharing' import type { AnyRoute, ReactNode } from './route' import type { + AllContext, + AllLoaderData, + AllParams, ControlledPromise, DeepPartial, - NoInfer, - ResolveRelativePath, - StaticDataRouteOption, -} from '@tanstack/router-core' -import type { AnyRouter, RegisteredRouter, RouterState } from './router' -import type { + FullSearchSchema, MakeOptionalPathParams, MakeOptionalSearchParams, MaskOptions, - ResolveRoute, - ToSubOptionsProps, -} from './link' -import type { - AllContext, - AllLoaderData, - AllParams, - FullSearchSchema, + NoInfer, ParseRoute, + ResolveRelativePath, + ResolveRoute, RouteById, RouteByPath, RouteIds, -} from './routeInfo' + StaticDataRouteOption, + ToSubOptionsProps, +} from '@tanstack/router-core' +import type { AnyRouter, RegisteredRouter, RouterState } from './router' export type MakeRouteMatchFromRoute = RouteMatch< TRoute['types']['id'], diff --git a/packages/react-router/src/RouterProvider.tsx b/packages/react-router/src/RouterProvider.tsx index ad499eaa896..45337512c85 100644 --- a/packages/react-router/src/RouterProvider.tsx +++ b/packages/react-router/src/RouterProvider.tsx @@ -1,12 +1,6 @@ import * as React from 'react' import { Matches } from './Matches' import { getRouterContext } from './routerContext' -import type { NavigateOptions, ToOptions } from './link' -import type { - ParsedLocation, - ViewTransitionOptions, -} from '@tanstack/router-core' -import type { RoutePaths } from './routeInfo' import type { AnyRouter, RegisteredRouter, @@ -14,48 +8,6 @@ import type { RouterOptions, } from './router' -export interface CommitLocationOptions { - replace?: boolean - resetScroll?: boolean - hashScrollIntoView?: boolean | ScrollIntoViewOptions - viewTransition?: boolean | ViewTransitionOptions - /** - * @deprecated All navigations use React transitions under the hood now - **/ - startTransition?: boolean - ignoreBlocker?: boolean -} - -export interface MatchLocation { - to?: string | number | null - fuzzy?: boolean - caseSensitive?: boolean - from?: string -} - -export type NavigateFn = < - TRouter extends RegisteredRouter, - TTo extends string | undefined, - TFrom extends RoutePaths | string = string, - TMaskFrom extends RoutePaths | string = TFrom, - TMaskTo extends string = '', ->( - opts: NavigateOptions, -) => Promise | void - -export type BuildLocationFn = < - TRouter extends RegisteredRouter, - TTo extends string | undefined, - TFrom extends RoutePaths | string = string, - TMaskFrom extends RoutePaths | string = TFrom, - TMaskTo extends string = '', ->( - opts: ToOptions & { - leaveParams?: boolean - _includeValidateSearch?: boolean - }, -) => ParsedLocation - export function RouterContextProvider< TRouter extends AnyRouter = RegisteredRouter, TDehydrated extends Record = Record, diff --git a/packages/react-router/src/fileRoute.ts b/packages/react-router/src/fileRoute.ts index 015856fa841..82770727671 100644 --- a/packages/react-router/src/fileRoute.ts +++ b/packages/react-router/src/fileRoute.ts @@ -10,58 +10,30 @@ import { useNavigate } from './useNavigate' import type { UseParamsRoute } from './useParams' import type { UseMatchRoute } from './useMatch' import type { UseSearchRoute } from './useSearch' -import type { - AnyContext, - AnyPathParams, - AnyValidator, - Constrain, - ResolveParams, -} from '@tanstack/router-core' import type { AnyRoute, FileBaseRouteOptions, - RootRoute, Route, RouteConstraints, RouteLoaderFn, UpdatableRouteOptions, } from './route' import type { RegisteredRouter } from './router' -import type { RouteById, RouteIds } from './routeInfo' +import type { + AnyContext, + AnyPathParams, + AnyValidator, + Constrain, + FileRoutesByPath, + ResolveParams, + RouteById, + RouteIds, +} from '@tanstack/router-core' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' import type { UseRouteContextRoute } from './useRouteContext' -export interface FileRoutesByPath { - // '/': { - // parentRoute: typeof rootRoute - // } -} - -export interface FileRouteTypes { - fileRoutesByFullPath: any - fullPaths: any - to: any - fileRoutesByTo: any - id: any - fileRoutesById: any -} - -export type InferFileRouteTypes = - TRouteTree extends RootRoute< - any, - any, - any, - any, - any, - any, - any, - infer TFileRouteTypes extends FileRouteTypes - > - ? TFileRouteTypes - : never - export function createFileRoute< TFilePath extends keyof FileRoutesByPath, TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'], diff --git a/packages/react-router/src/index.tsx b/packages/react-router/src/index.tsx index 1b335cc03c6..aadd9085b38 100644 --- a/packages/react-router/src/index.tsx +++ b/packages/react-router/src/index.tsx @@ -106,6 +106,7 @@ export type { IntersectAssign, ResolveValidatorInput, ResolveValidatorOutput, + Register, AnyValidator, DefaultValidator, ValidatorFn, @@ -120,6 +121,8 @@ export type { Validator, ValidatorAdapter, ValidatorObj, + FileRoutesByPath, + RouteById, } from '@tanstack/router-core' export { @@ -150,11 +153,7 @@ export { createLazyRoute, createLazyFileRoute, } from './fileRoute' -export type { - FileRoutesByPath, - FileRouteTypes, - LazyRouteOptions, -} from './fileRoute' +export type { LazyRouteOptions } from './fileRoute' export * from './history' @@ -177,13 +176,33 @@ export type { PathParamOptions, ToPathOption, LinkOptions, + MakeOptionalPathParams, + FileRouteTypes, + RouteContextParameter, + BeforeLoadContextParameter, + ResolveAllContext, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveFullSearchSchemaInput, + RouteIds, + NavigateFn, + BuildLocationFn, + FullSearchSchemaOption, + MakeRemountDepsOptionsUnion, + RemountDepsOptions, + ResolveFullPath, + AnyRouteWithContext, + AnyRouterWithContext, + CommitLocationOptions, + MatchLocation, +} from '@tanstack/router-core' +export type { UseLinkPropsOptions, ActiveLinkOptions, LinkProps, LinkComponent, LinkComponentProps, CreateLinkProps, - MakeOptionalPathParams, } from './link' export { @@ -234,12 +253,9 @@ export type { UpdatableRouteOptions, RouteLoaderFn, LoaderFnContext, - ResolveFullSearchSchema, - ResolveFullSearchSchemaInput, AnyRoute, RouteConstraints, AnyRootRoute, - ResolveFullPath, RouteMask, ReactNode, SyncRouteComponent, @@ -248,36 +264,13 @@ export type { ErrorRouteComponent, NotFoundRouteComponent, RootRouteOptions, - AnyRouteWithContext, - FullSearchSchemaOption, RouteContextFn, RouteContextOptions, BeforeLoadFn, BeforeLoadContextOptions, ContextOptions, - RouteContextParameter, - BeforeLoadContextParameter, - ResolveAllContext, - ResolveAllParamsFromParent, - MakeRemountDepsOptionsUnion, - RemountDepsOptions, } from './route' -export type { - ParseRoute, - RoutesById, - RouteById, - RouteIds, - RoutesByPath, - RouteByPath, - RoutePaths, - FullSearchSchema, - AllParams, - AllLoaderData, - FullSearchSchemaInput, - AllContext, -} from './routeInfo' - export { componentTypes, createRouter, @@ -289,7 +282,6 @@ export { } from './router' export type { - Register, AnyRouter, RegisteredRouter, RouterContextOptions, @@ -302,19 +294,12 @@ export type { RouterEvents, RouterEvent, RouterListener, - AnyRouterWithContext, ControllablePromise, InjectedHtmlEntry, } from './router' export { RouterProvider, RouterContextProvider } from './RouterProvider' -export type { - RouterProps, - CommitLocationOptions, - MatchLocation, - NavigateFn, - BuildLocationFn, -} from './RouterProvider' +export type { RouterProps } from './RouterProvider' export { useElementScrollRestoration, diff --git a/packages/react-router/src/link.tsx b/packages/react-router/src/link.tsx index 9496273f480..8ef7549563a 100644 --- a/packages/react-router/src/link.tsx +++ b/packages/react-router/src/link.tsx @@ -19,35 +19,11 @@ import { import { useMatch } from './useMatch' import type { Constrain, - ConstrainLiteral, - Expand, - IsRequiredParams, - LinkOptionsProps, - MakeDifferenceOptional, - NoInfer, - NonNullableUpdater, - ParsedLocation, - PickRequired, - RemoveTrailingSlashes, - ResolveRelativePath, - Updater, - ViewTransitionOptions, - WithoutEmpty, -} from '@tanstack/router-core' -import type { HistoryState, ParsedHistoryState } from '@tanstack/history' -import type { - AllParams, - CatchAllPaths, - CurrentPath, - FullSearchSchema, - FullSearchSchemaInput, - ParentPath, - RouteByPath, - RouteByToPath, + LinkCurrentTargetElement, + LinkOptions, RoutePaths, - RouteToPath, - ToPath, -} from './routeInfo' +} from '@tanstack/router-core' + import type { AnyRouter, RegisteredRouter } from './router' import type { ReactNode } from 'react' import type { @@ -55,405 +31,6 @@ import type { ValidateLinkOptionsArray, } from './typePrimitives' -export type FindDescendantToPaths< - TRouter extends AnyRouter, - TPrefix extends string, -> = `${TPrefix}/${string}` & RouteToPath - -export type InferDescendantToPaths< - TRouter extends AnyRouter, - TPrefix extends string, - TPaths = FindDescendantToPaths, -> = TPaths extends `${TPrefix}/` - ? never - : TPaths extends `${TPrefix}/${infer TRest}` - ? TRest - : never - -export type RelativeToPath< - TRouter extends AnyRouter, - TTo extends string, - TResolvedPath extends string, -> = - | (TResolvedPath & RouteToPath extends never - ? never - : ToPath) - | `${RemoveTrailingSlashes}/${InferDescendantToPaths>}` - -export type RelativeToParentPath< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string, - TResolvedPath extends string = ResolveRelativePath, -> = - | RelativeToPath - | (TTo extends `${string}..` | `${string}../` - ? TResolvedPath extends '/' | '' - ? never - : FindDescendantToPaths< - TRouter, - RemoveTrailingSlashes - > extends never - ? never - : `${RemoveTrailingSlashes}/${ParentPath}` - : never) - -export type RelativeToCurrentPath< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string, - TResolvedPath extends string = ResolveRelativePath, -> = RelativeToPath | CurrentPath - -export type AbsoluteToPath = - | (string extends TFrom - ? CurrentPath - : TFrom extends `/` - ? never - : CurrentPath) - | (string extends TFrom - ? ParentPath - : TFrom extends `/` - ? never - : ParentPath) - | RouteToPath - | (TFrom extends '/' - ? never - : string extends TFrom - ? never - : InferDescendantToPaths>) - -export type RelativeToPathAutoComplete< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string, -> = string extends TTo - ? string - : string extends TFrom - ? AbsoluteToPath - : TTo & `..${string}` extends never - ? TTo & `.${string}` extends never - ? AbsoluteToPath - : RelativeToCurrentPath - : RelativeToParentPath - -export type NavigateOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', - TMaskFrom extends string = TFrom, - TMaskTo extends string = '.', -> = ToOptions & NavigateOptionProps - -export interface NavigateOptionProps { - // if set to `true`, the router will scroll the element with an id matching the hash into view with default ScrollIntoViewOptions. - // if set to `false`, the router will not scroll the element with an id matching the hash into view. - // if set to `ScrollIntoViewOptions`, the router will scroll the element with an id matching the hash into view with the provided options. - hashScrollIntoView?: boolean | ScrollIntoViewOptions - // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one. - replace?: boolean - resetScroll?: boolean - /** @deprecated All navigations now use startTransition under the hood */ - startTransition?: boolean - // if set to `true`, the router will wrap the resulting navigation in a document.startViewTransition() call. - // if set to `ViewTransitionOptions`, the router will pass the `types` field to document.startViewTransition({update: fn, types: viewTransition.types}) call - viewTransition?: boolean | ViewTransitionOptions - ignoreBlocker?: boolean - reloadDocument?: boolean - href?: string -} - -export type ToOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', - TMaskFrom extends string = TFrom, - TMaskTo extends string = '.', -> = ToSubOptions & MaskOptions - -export interface MaskOptions< - in out TRouter extends AnyRouter, - in out TMaskFrom extends string, - in out TMaskTo extends string, -> { - _fromLocation?: ParsedLocation - mask?: ToMaskOptions -} - -export type ToMaskOptions< - TRouter extends AnyRouter = RegisteredRouter, - TMaskFrom extends string = string, - TMaskTo extends string = '.', -> = ToSubOptions & { - unmaskOnReload?: boolean -} - -export type ToSubOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', -> = ToSubOptionsProps & - SearchParamOptions & - PathParamOptions - -export interface RequiredToOptions< - in out TRouter extends AnyRouter, - in out TFrom extends string, - in out TTo extends string | undefined, -> { - to: ToPathOption & {} -} - -export interface OptionalToOptions< - in out TRouter extends AnyRouter, - in out TFrom extends string, - in out TTo extends string | undefined, -> { - to?: ToPathOption & {} -} - -export type MakeToRequired< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string | undefined, -> = string extends TFrom - ? string extends TTo - ? OptionalToOptions - : TTo & CatchAllPaths extends never - ? RequiredToOptions - : OptionalToOptions - : OptionalToOptions - -export type ToSubOptionsProps< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends RoutePaths | string = string, - TTo extends string | undefined = '.', -> = MakeToRequired & { - hash?: true | Updater - state?: true | NonNullableUpdater - from?: FromPathOption & {} -} - -export type ParamsReducerFn< - in out TRouter extends AnyRouter, - in out TParamVariant extends ParamVariant, - in out TFrom, - in out TTo, -> = ( - current: Expand>, -) => Expand> - -type ParamsReducer< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - | Expand> - | (ParamsReducerFn & {}) - -type ParamVariant = 'PATH' | 'SEARCH' - -export type ResolveRoute< - TRouter extends AnyRouter, - TFrom, - TTo, - TPath = ResolveRelativePath, -> = TPath extends string - ? TFrom extends TPath - ? RouteByPath - : RouteByToPath - : never - -type ResolveFromParamType = - TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchema' - -type ResolveFromAllParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, -> = TParamVariant extends 'PATH' - ? AllParams - : FullSearchSchema - -type ResolveFromParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, -> = string extends TFrom - ? ResolveFromAllParams - : RouteByPath< - TRouter['routeTree'], - TFrom - >['types'][ResolveFromParamType] - -type ResolveToParamType = - TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchemaInput' - -type ResolveAllToParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, -> = TParamVariant extends 'PATH' - ? AllParams - : FullSearchSchemaInput - -export type ResolveToParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - ResolveRelativePath extends infer TPath - ? undefined extends TPath - ? never - : string extends TPath - ? ResolveAllToParams - : TPath extends CatchAllPaths - ? ResolveAllToParams - : ResolveRoute< - TRouter, - TFrom, - TTo - >['types'][ResolveToParamType] - : never - -type ResolveRelativeToParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, - TToParams = ResolveToParams, -> = TParamVariant extends 'SEARCH' - ? TToParams - : string extends TFrom - ? TToParams - : MakeDifferenceOptional< - ResolveFromParams, - TToParams - > - -export interface MakeOptionalSearchParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - search?: true | (ParamsReducer & {}) -} - -export interface MakeOptionalPathParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - params?: true | (ParamsReducer & {}) -} - -type MakeRequiredParamsReducer< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - | (string extends TFrom - ? never - : ResolveFromParams extends WithoutEmpty< - PickRequired< - ResolveRelativeToParams - > - > - ? true - : never) - | (ParamsReducer & {}) - -export interface MakeRequiredPathParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - params: MakeRequiredParamsReducer & {} -} - -export interface MakeRequiredSearchParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - search: MakeRequiredParamsReducer & {} -} - -export type IsRequired< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - ResolveRelativePath extends infer TPath - ? undefined extends TPath - ? never - : TPath extends CatchAllPaths - ? never - : IsRequiredParams< - ResolveRelativeToParams - > - : never - -export type SearchParamOptions = - IsRequired extends never - ? MakeOptionalSearchParams - : MakeRequiredSearchParams - -export type PathParamOptions = - IsRequired extends never - ? MakeOptionalPathParams - : MakeRequiredPathParams - -export type ToPathOption< - TRouter extends AnyRouter = AnyRouter, - TFrom extends string = string, - TTo extends string | undefined = string, -> = ConstrainLiteral< - TTo, - RelativeToPathAutoComplete< - TRouter, - NoInfer extends string ? NoInfer : '', - NoInfer & string - > -> - -export type FromPathOption = ConstrainLiteral< - TFrom, - RoutePaths -> - -export type LinkOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', - TMaskFrom extends string = TFrom, - TMaskTo extends string = '.', -> = NavigateOptions & LinkOptionsProps - -// type Test1 = ResolveRelativePath<'/', '/posts'> -// // ^? -// type Test4 = ResolveRelativePath<'/posts/1/comments', '../..'> -// // ^? -// type Test5 = ResolveRelativePath<'/posts/1/comments', '../../..'> -// // ^? -// type Test6 = ResolveRelativePath<'/posts/1/comments', './1'> -// // ^? -// type Test7 = ResolveRelativePath<'/posts/1/comments', './1/2'> -// // ^? -// type Test8 = ResolveRelativePath<'/posts/1/comments', '../edit'> -// // ^? -// type Test9 = ResolveRelativePath<'/posts/1/comments', '1'> -// // ^? -// type Test10 = ResolveRelativePath<'/posts/1/comments', './1'> -// // ^? -// type Test11 = ResolveRelativePath<'/posts/1/comments', './1/2'> -// // ^? -type LinkCurrentTargetElement = { - preloadTimeout?: null | ReturnType -} - export function useLinkProps< TRouter extends AnyRouter = RegisteredRouter, const TFrom extends string = string, diff --git a/packages/react-router/src/not-found.tsx b/packages/react-router/src/not-found.tsx index 9db1d473e05..df5041e0a68 100644 --- a/packages/react-router/src/not-found.tsx +++ b/packages/react-router/src/not-found.tsx @@ -3,7 +3,7 @@ import { CatchBoundary } from './CatchBoundary' import { useRouterState } from './useRouterState' import type { ErrorInfo } from 'react' import type { RegisteredRouter } from './router' -import type { RouteIds } from './routeInfo' +import type { RouteIds } from '@tanstack/router-core' export type NotFoundError = { /** diff --git a/packages/react-router/src/redirects.ts b/packages/react-router/src/redirects.ts index 9f90c2434c8..348d7aa40d6 100644 --- a/packages/react-router/src/redirects.ts +++ b/packages/react-router/src/redirects.ts @@ -1,7 +1,9 @@ -import type { NavigateOptions } from './link' -import type { RoutePaths } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' -import type { PickAsRequired } from '@tanstack/router-core' +import type { + NavigateOptions, + PickAsRequired, + RoutePaths, +} from '@tanstack/router-core' export type AnyRedirect = Redirect diff --git a/packages/react-router/src/route.ts b/packages/react-router/src/route.ts index ffa9d57a41c..eafee6a61a1 100644 --- a/packages/react-router/src/route.ts +++ b/packages/react-router/src/route.ts @@ -12,36 +12,43 @@ import type { AnyPathParams, AnySchema, AnyValidator, - Assign, + BeforeLoadContextParameter, + BuildLocationFn, Constrain, ConstrainLiteral, - ContextAsyncReturnType, - ContextReturnType, + RootRoute as CoreRootRoute, + Route as CoreRoute, DefaultValidator, ErrorComponentProps, Expand, - InferAllContext, - InferAllParams, - InferFullSearchSchema, - InferFullSearchSchemaInput, - IntersectAssign, + FullSearchSchemaOption, + NavigateFn, + NavigateOptions, NoInfer, NotFoundRouteProps, ParamsOptions, ParsedLocation, + RemountDepsOptions, + ResolveAllContext, + ResolveAllParamsFromParent, + ResolveFullPath, + ResolveFullSearchSchema, + ResolveFullSearchSchemaInput, ResolveId, ResolveLoaderData, ResolveParams, - ResolveRouteContext, - ResolveSearchValidatorInput, - ResolveValidatorOutput, RootRouteId, + RouteById, RouteContext, + RouteContextParameter, + RouteIds, RoutePathOptions, RoutePathOptionsIntersection, - RoutePrefix, + RoutePaths, + RouteTypes, SearchFilter, SearchMiddleware, + ToMaskOptions, TrimPathRight, UpdatableStaticRouteOption, } from '@tanstack/router-core' @@ -58,10 +65,7 @@ import type { MakeRouteMatchUnion, RouteMatch, } from './Matches' -import type { NavigateOptions, ToMaskOptions } from './link' -import type { ParseRoute, RouteById, RouteIds, RoutePaths } from './routeInfo' import type { AnyRouter, RegisteredRouter, Router } from './router' -import type { BuildLocationFn, NavigateFn } from './RouterProvider' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' @@ -106,13 +110,6 @@ export type RouteOptions< NoInfer > -export interface FullSearchSchemaOption< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, -> { - search: Expand> -} - export type RouteContextFn< in out TParentRoute extends AnyRoute, in out TSearchValidator, @@ -288,30 +285,6 @@ export interface RouteContextOptions< context: Expand> } -export interface RemountDepsOptions< - in out TRouteId, - in out TFullSearchSchema, - in out TAllParams, - in out TLoaderDeps, -> { - routeId: TRouteId - search: TFullSearchSchema - params: TAllParams - loaderDeps: TLoaderDeps -} - -export type MakeRemountDepsOptionsUnion< - TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], - TRoute extends AnyRoute = ParseRoute, -> = TRoute extends any - ? RemountDepsOptions< - TRoute['id'], - TRoute['types']['fullSearchSchema'], - TRoute['types']['allParams'], - TRoute['types']['loaderDeps'] - > - : never - export interface BeforeLoadContextOptions< in out TParentRoute extends AnyRoute, in out TSearchValidator, @@ -570,50 +543,6 @@ export interface LoaderFnContext< route: Route } -export type ResolveFullSearchSchema< - TParentRoute extends AnyRoute, - TSearchValidator, -> = unknown extends TParentRoute - ? ResolveValidatorOutput - : IntersectAssign< - InferFullSearchSchema, - ResolveValidatorOutput - > - -export type ResolveFullSearchSchemaInput< - TParentRoute extends AnyRoute, - TSearchValidator, -> = IntersectAssign< - InferFullSearchSchemaInput, - ResolveSearchValidatorInput -> - -export type RouteContextParameter< - TParentRoute extends AnyRoute, - TRouterContext, -> = unknown extends TParentRoute - ? TRouterContext - : Assign> - -export type BeforeLoadContextParameter< - TParentRoute extends AnyRoute, - TRouterContext, - TRouteContextFn, -> = Assign< - RouteContextParameter, - ContextReturnType -> - -export type ResolveAllContext< - TParentRoute extends AnyRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, -> = Assign< - BeforeLoadContextParameter, - ContextAsyncReturnType -> - export interface AnyRoute extends Route< any, @@ -628,18 +557,10 @@ export interface AnyRoute any, any, any, + any, any > {} -export type AnyRouteWithContext = AnyRoute & { - types: { allContext: TContext } -} - -export type ResolveAllParamsFromParent< - TParentRoute extends AnyRoute, - TParams, -> = Assign, TParams> - export type RouteConstraints = { TParentRoute: AnyRoute TPath: string @@ -753,7 +674,25 @@ export class Route< in out TLoaderDeps extends Record = {}, in out TLoaderFn = undefined, in out TChildren = unknown, -> { + in out TFileRouteTypes = unknown, +> implements + CoreRoute< + TParentRoute, + TPath, + TFullPath, + TCustomId, + TId, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > +{ isRoot: TParentRoute extends Route ? true : false options: RouteOptions< TParentRoute, @@ -852,37 +791,22 @@ export class Route< ;(this as any).$$typeof = Symbol.for('react.memo') } - types!: { - parentRoute: TParentRoute - path: TPath - to: TrimPathRight - fullPath: TFullPath - customId: TCustomId - id: TId - searchSchema: ResolveValidatorOutput - searchSchemaInput: ResolveSearchValidatorInput - searchValidator: TSearchValidator - fullSearchSchema: ResolveFullSearchSchema - fullSearchSchemaInput: ResolveFullSearchSchemaInput< - TParentRoute, - TSearchValidator - > - params: TParams - allParams: ResolveAllParamsFromParent - routerContext: TRouterContext - routeContext: ResolveRouteContext - routeContextFn: TRouteContextFn - beforeLoadFn: TBeforeLoadFn - allContext: ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - > - children: TChildren - loaderData: ResolveLoaderData - loaderDeps: TLoaderDeps - } + types!: RouteTypes< + TParentRoute, + TPath, + TFullPath, + TCustomId, + TId, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > init = (opts: { originalIndex: number; defaultSsr?: boolean }): void => { this.originalIndex = opts.originalIndex @@ -973,7 +897,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > { return this._addFileChildren(children) as Route< TParentRoute, @@ -988,7 +913,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > } @@ -1007,7 +933,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > { if (Array.isArray(children)) { this.children = children as TChildren @@ -1030,7 +957,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > } @@ -1062,7 +990,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TNewLoaderFn, - TChildren + TChildren, + TFileRouteTypes > } @@ -1256,29 +1185,43 @@ export function createRootRouteWithContext() { export const rootRouteWithContext = createRootRouteWithContext export class RootRoute< - in out TSearchValidator = undefined, - in out TRouterContext = {}, - in out TRouteContextFn = AnyContext, - in out TBeforeLoadFn = AnyContext, - in out TLoaderDeps extends Record = {}, - in out TLoaderFn = undefined, - in out TChildren = unknown, - in out TFileRouteTypes = unknown, -> extends Route< - any, // TParentRoute - '/', // TPath - '/', // TFullPath - string, // TCustomId - RootRouteId, // TId - TSearchValidator, // TSearchValidator - {}, // TParams - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, - TLoaderDeps, - TLoaderFn, - TChildren // TChildren -> { + in out TSearchValidator = undefined, + in out TRouterContext = {}, + in out TRouteContextFn = AnyContext, + in out TBeforeLoadFn = AnyContext, + in out TLoaderDeps extends Record = {}, + in out TLoaderFn = undefined, + in out TChildren = unknown, + in out TFileRouteTypes = unknown, + > + extends Route< + any, // TParentRoute + '/', // TPath + '/', // TFullPath + string, // TCustomId + RootRouteId, // TId + TSearchValidator, // TSearchValidator + {}, // TParams + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, // TChildren + TFileRouteTypes + > + implements + CoreRootRoute< + TSearchValidator, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > +{ /** * @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead. */ @@ -1389,12 +1332,6 @@ export function createRootRoute< >(options) } -export type ResolveFullPath< - TParentRoute extends AnyRoute, - TPath extends string, - TPrefixed = RoutePrefix, -> = TPrefixed extends RootRouteId ? '/' : TPrefixed - export type RouteMask = { routeTree: TRouteTree from: RoutePaths diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts index 31eb4fe793c..5056de5100b 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -43,28 +43,37 @@ import type { AnyContext, AnySchema, AnyValidator, + BuildLocationFn, CommitLocationOptions, ControlledPromise, + FullSearchSchema, + MakeRemountDepsOptionsUnion, Manifest, + NavigateFn, + NavigateOptions, NonNullableUpdater, ParsedLocation, PickAsRequired, + Register, ResolveRelativePath, + RouteById, + RoutePaths, + RoutesById, + RoutesByPath, SearchMiddleware, SearchParser, SearchSerializer, StartSerializer, + ToOptions, TrailingSlashOption, Updater, ViewTransitionOptions, } from '@tanstack/router-core' import type { AnyRoute, - AnyRouteWithContext, BeforeLoadContextOptions, ErrorRouteComponent, LoaderFnContext, - MakeRemountDepsOptionsUnion, NotFoundRouteComponent, RootRoute, RouteComponent, @@ -72,13 +81,6 @@ import type { RouteMask, } from './route' -import type { - FullSearchSchema, - RouteById, - RoutePaths, - RoutesById, - RoutesByPath, -} from './routeInfo' import type { AnyRouteMatch, MakeRouteMatch, @@ -86,11 +88,8 @@ import type { MatchRouteOptions, } from './Matches' -import type { BuildLocationFn, NavigateFn } from './RouterProvider' - import type { AnyRedirect, ResolvedRedirect } from './redirects' import type { NotFoundError } from './not-found' -import type { NavigateOptions, ToOptions } from './link' declare global { interface Window { @@ -98,20 +97,8 @@ declare global { } } -export interface Register { - // router: Router -} - export type AnyRouter = Router -export type AnyRouterWithContext = Router< - AnyRouteWithContext, - any, - any, - any, - any -> - export type RegisteredRouter = Register extends { router: infer TRouter extends AnyRouter } diff --git a/packages/react-router/src/typePrimitives.ts b/packages/react-router/src/typePrimitives.ts index a774361a475..f6b100a0d3a 100644 --- a/packages/react-router/src/typePrimitives.ts +++ b/packages/react-router/src/typePrimitives.ts @@ -1,17 +1,18 @@ import type { + Constrain, + ConstrainLiteral, FromPathOption, - LinkComponentProps, NavigateOptions, PathParamOptions, + RouteIds, SearchParamOptions, ToPathOption, -} from './link' +} from '@tanstack/router-core' +import type { LinkComponentProps } from './link' import type { Redirect } from './redirects' -import type { RouteIds } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { UseParamsOptions, UseParamsResult } from './useParams' import type { UseSearchOptions, UseSearchResult } from './useSearch' -import type { Constrain, ConstrainLiteral } from '@tanstack/router-core' export type ValidateFromPath< TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/react-router/src/useBlocker.tsx b/packages/react-router/src/useBlocker.tsx index 0bcf5ec845f..ef6a63e4618 100644 --- a/packages/react-router/src/useBlocker.tsx +++ b/packages/react-router/src/useBlocker.tsx @@ -6,7 +6,7 @@ import type { HistoryLocation, } from '@tanstack/history' import type { AnyRoute } from './route' -import type { ParseRoute } from './routeInfo' +import type { ParseRoute } from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' interface ShouldBlockFnLocation< diff --git a/packages/react-router/src/useLoaderData.tsx b/packages/react-router/src/useLoaderData.tsx index 538a3578af8..d979492bf63 100644 --- a/packages/react-router/src/useLoaderData.tsx +++ b/packages/react-router/src/useLoaderData.tsx @@ -4,9 +4,8 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { AllLoaderData, RouteById } from './routeInfo' +import type { AllLoaderData, Expand, RouteById } from '@tanstack/router-core' import type { StrictOrFrom } from './utils' -import type { Expand } from '@tanstack/router-core' export interface UseLoaderDataBaseOptions< TRouter extends AnyRouter, diff --git a/packages/react-router/src/useLoaderDeps.tsx b/packages/react-router/src/useLoaderDeps.tsx index bb176cb2f85..326bec856fe 100644 --- a/packages/react-router/src/useLoaderDeps.tsx +++ b/packages/react-router/src/useLoaderDeps.tsx @@ -4,9 +4,8 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { RouteById } from './routeInfo' import type { StrictOrFrom } from './utils' -import type { Expand } from '@tanstack/router-core' +import type { Expand, RouteById } from '@tanstack/router-core' export interface UseLoaderDepsBaseOptions< TRouter extends AnyRouter, diff --git a/packages/react-router/src/useNavigate.tsx b/packages/react-router/src/useNavigate.tsx index ef7e7cd3305..4d77a52c309 100644 --- a/packages/react-router/src/useNavigate.tsx +++ b/packages/react-router/src/useNavigate.tsx @@ -1,6 +1,6 @@ import * as React from 'react' import { useRouter } from './useRouter' -import type { FromPathOption, NavigateOptions } from './link' +import type { FromPathOption, NavigateOptions } from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' export type UseNavigateResult = < diff --git a/packages/react-router/src/useParams.tsx b/packages/react-router/src/useParams.tsx index 25f99e2ef6d..82bbe015550 100644 --- a/packages/react-router/src/useParams.tsx +++ b/packages/react-router/src/useParams.tsx @@ -4,10 +4,14 @@ import type { StructuralSharingOption, ValidateSelected, } from './structuralSharing' -import type { AllParams, RouteById } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand, ThrowOrOptional } from '@tanstack/router-core' +import type { + AllParams, + Expand, + RouteById, + ThrowOrOptional, +} from '@tanstack/router-core' export interface UseParamsBaseOptions< TRouter extends AnyRouter, diff --git a/packages/react-router/src/useRouteContext.ts b/packages/react-router/src/useRouteContext.ts index dd86b766a2a..bfc0f32bf97 100644 --- a/packages/react-router/src/useRouteContext.ts +++ b/packages/react-router/src/useRouteContext.ts @@ -1,8 +1,7 @@ import { useMatch } from './useMatch' -import type { AllContext, RouteById } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand } from '@tanstack/router-core' +import type { AllContext, Expand, RouteById } from '@tanstack/router-core' export interface UseRouteContextBaseOptions< TRouter extends AnyRouter, diff --git a/packages/react-router/src/useSearch.tsx b/packages/react-router/src/useSearch.tsx index eb3aaae4cd1..4b36565243b 100644 --- a/packages/react-router/src/useSearch.tsx +++ b/packages/react-router/src/useSearch.tsx @@ -4,10 +4,14 @@ import type { StructuralSharingOption, ValidateSelected, } from './structuralSharing' -import type { FullSearchSchema, RouteById } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand, ThrowOrOptional } from '@tanstack/router-core' +import type { + Expand, + FullSearchSchema, + RouteById, + ThrowOrOptional, +} from '@tanstack/router-core' export interface UseSearchBaseOptions< TRouter extends AnyRouter, diff --git a/packages/react-router/src/utils.ts b/packages/react-router/src/utils.ts index baa0695c6e9..048f5c0f68c 100644 --- a/packages/react-router/src/utils.ts +++ b/packages/react-router/src/utils.ts @@ -1,7 +1,9 @@ import * as React from 'react' -import type { RouteIds } from './routeInfo' -import type { AnyRouter } from './router' -import type { ConstrainLiteral } from '@tanstack/router-core' +import type { + AnyRouter, + ConstrainLiteral, + RouteIds, +} from '@tanstack/router-core' export function useStableCallback) => any>( fn: T, diff --git a/packages/react-router/tests/fileRoute.test-d.tsx b/packages/react-router/tests/fileRoute.test-d.tsx index f2f7fccc42a..c97d9323252 100644 --- a/packages/react-router/tests/fileRoute.test-d.tsx +++ b/packages/react-router/tests/fileRoute.test-d.tsx @@ -17,7 +17,7 @@ const postRoute = createFileRoute('/_postLayout/posts/$postId_')() const protectedRoute = createFileRoute('/(auth)/protected')() -declare module '../src/fileRoute' { +declare module '@tanstack/router-core' { interface FileRoutesByPath { '/': { preLoaderRoute: typeof indexRoute diff --git a/packages/router-core/src/RouterProvider.ts b/packages/router-core/src/RouterProvider.ts index 4e7e3d9f821..1c2b42ef219 100644 --- a/packages/router-core/src/RouterProvider.ts +++ b/packages/router-core/src/RouterProvider.ts @@ -1,4 +1,11 @@ -import type { ViewTransitionOptions } from './router' +import type { NavigateOptions, ToOptions } from './link' +import type { ParsedLocation } from './location' +import type { RoutePaths } from './routeInfo' +import type { + AnyRouter, + RegisteredRouter, + ViewTransitionOptions, +} from './router' export interface MatchLocation { to?: string | number | null @@ -18,3 +25,26 @@ export interface CommitLocationOptions { startTransition?: boolean ignoreBlocker?: boolean } + +export type NavigateFn = < + TRouter extends RegisteredRouter, + TTo extends string | undefined, + TFrom extends RoutePaths | string = string, + TMaskFrom extends RoutePaths | string = TFrom, + TMaskTo extends string = '', +>( + opts: NavigateOptions, +) => Promise | void + +export type BuildLocationFn = < + TRouter extends AnyRouter, + TTo extends string | undefined, + TFrom extends RoutePaths | string = string, + TMaskFrom extends RoutePaths | string = TFrom, + TMaskTo extends string = '', +>( + opts: ToOptions & { + leaveParams?: boolean + _includeValidateSearch?: boolean + }, +) => ParsedLocation diff --git a/packages/router-core/src/fileRoute.ts b/packages/router-core/src/fileRoute.ts new file mode 100644 index 00000000000..45cd3dd2c32 --- /dev/null +++ b/packages/router-core/src/fileRoute.ts @@ -0,0 +1,32 @@ +import type { AnyRoute, RootRoute } from './route' + +export interface FileRouteTypes { + fileRoutesByFullPath: any + fullPaths: any + to: any + fileRoutesByTo: any + id: any + fileRoutesById: any +} + +export type InferFileRouteTypes = + TRouteTree extends RootRoute< + any, + any, + any, + any, + any, + any, + any, + infer TFileRouteTypes extends FileRouteTypes + > + ? unknown extends TFileRouteTypes + ? never + : TFileRouteTypes + : never + +export interface FileRoutesByPath { + // '/': { + // parentRoute: typeof rootRoute + // } +} diff --git a/packages/router-core/src/index.ts b/packages/router-core/src/index.ts index aa3281be7b8..4f7a7797f29 100644 --- a/packages/router-core/src/index.ts +++ b/packages/router-core/src/index.ts @@ -14,8 +14,54 @@ export type { ResolveParentPath, ResolveRelativePath, LinkCurrentTargetElement, + FindDescendantToPaths, + InferDescendantToPaths, + RelativeToPath, + RelativeToParentPath, + RelativeToCurrentPath, + AbsoluteToPath, + RelativeToPathAutoComplete, + NavigateOptions, + ToOptions, + ToMaskOptions, + ToSubOptions, + ResolveRoute, + SearchParamOptions, + PathParamOptions, + ToPathOption, + LinkOptions, + MakeOptionalPathParams, + FromPathOption, + MakeOptionalSearchParams, + MaskOptions, + ToSubOptionsProps, + RequiredToOptions, } from './link' +export type { + RouteToPath, + TrailingSlashOptionByRouter, + ParseRoute, + CodeRouteToPath, + RouteIds, + FullSearchSchema, + FullSearchSchemaInput, + AllParams, + RouteById, + AllContext, + RoutePaths, + RoutesById, + RoutesByPath, + AllLoaderData, + RouteByPath, +} from './routeInfo' + +export type { + InferFileRouteTypes, + FileRouteTypes, + FileRoutesByPath, +} from './fileRoute' + export type { StartSerializer, Serializable, @@ -106,6 +152,21 @@ export type { ResolveSearchSchemaInput, ResolveSearchSchemaFn, ResolveSearchSchema, + ResolveFullSearchSchema, + ResolveFullSearchSchemaInput, + ResolveAllContext, + BeforeLoadContextParameter, + RouteContextParameter, + ResolveAllParamsFromParent, + AnyRoute, + Route, + RootRoute, + RouteTypes, + FullSearchSchemaOption, + RemountDepsOptions, + MakeRemountDepsOptionsUnion, + ResolveFullPath, + AnyRouteWithContext, } from './route' export { defaultSerializeError, getLocationChangeInfo } from './router' @@ -117,9 +178,17 @@ export type { ExtractedEntry, StreamState, TrailingSlashOption, + Register, + AnyRouter, + AnyRouterWithContext, } from './router' -export type { MatchLocation, CommitLocationOptions } from './RouterProvider' +export type { + MatchLocation, + CommitLocationOptions, + NavigateFn, + BuildLocationFn, +} from './RouterProvider' export { retainSearchParams, stripSearchParams } from './searchMiddleware' diff --git a/packages/router-core/src/link.ts b/packages/router-core/src/link.ts index 4a46fe27217..8db8faf8b3e 100644 --- a/packages/router-core/src/link.ts +++ b/packages/router-core/src/link.ts @@ -1,3 +1,34 @@ +import type { HistoryState, ParsedHistoryState } from '@tanstack/history' +import type { + AllParams, + CatchAllPaths, + CurrentPath, + FullSearchSchema, + FullSearchSchemaInput, + ParentPath, + RouteByPath, + RouteByToPath, + RoutePaths, + RouteToPath, + ToPath, +} from './routeInfo' +import type { + AnyRouter, + RegisteredRouter, + ViewTransitionOptions, +} from './router' +import type { + ConstrainLiteral, + Expand, + MakeDifferenceOptional, + NoInfer, + NonNullableUpdater, + PickRequired, + Updater, + WithoutEmpty, +} from './utils' +import type { ParsedLocation } from './location' + export type IsRequiredParams = Record extends TParams ? never : true @@ -32,43 +63,6 @@ export type RemoveLeadingSlashes = T & `/${string}` extends never ? R : T -export interface ActiveOptions { - exact?: boolean - includeHash?: boolean - includeSearch?: boolean - explicitUndefined?: boolean -} - -export interface LinkOptionsProps { - /** - * The standard anchor tag target attribute - */ - target?: HTMLAnchorElement['target'] - /** - * Configurable options to determine if the link should be considered active or not - * @default {exact:true,includeHash:true} - */ - activeOptions?: ActiveOptions - /** - * The preloading strategy for this link - * - `false` - No preloading - * - `'intent'` - Preload the linked route on hover and cache it for this many milliseconds in hopes that the user will eventually navigate there. - * - `'viewport'` - Preload the linked route when it enters the viewport - */ - preload?: false | 'intent' | 'viewport' | 'render' - /** - * When a preload strategy is set, this delays the preload by this many milliseconds. - * If the user exits the link before this delay, the preload will be cancelled. - */ - preloadDelay?: number - /** - * Control whether the link should be disabled or not - * If set to `true`, the link will be rendered without an `href` attribute - * @default false - */ - disabled?: boolean -} - type JoinPath = TRight extends '' ? TLeft : TLeft extends '' @@ -132,6 +126,420 @@ export type ResolveRelativePath = string extends TFrom : never : never +export type FindDescendantToPaths< + TRouter extends AnyRouter, + TPrefix extends string, +> = `${TPrefix}/${string}` & RouteToPath + +export type InferDescendantToPaths< + TRouter extends AnyRouter, + TPrefix extends string, + TPaths = FindDescendantToPaths, +> = TPaths extends `${TPrefix}/` + ? never + : TPaths extends `${TPrefix}/${infer TRest}` + ? TRest + : never + +export type RelativeToPath< + TRouter extends AnyRouter, + TTo extends string, + TResolvedPath extends string, +> = + | (TResolvedPath & RouteToPath extends never + ? never + : ToPath) + | `${RemoveTrailingSlashes}/${InferDescendantToPaths>}` + +export type RelativeToParentPath< + TRouter extends AnyRouter, + TFrom extends string, + TTo extends string, + TResolvedPath extends string = ResolveRelativePath, +> = + | RelativeToPath + | (TTo extends `${string}..` | `${string}../` + ? TResolvedPath extends '/' | '' + ? never + : FindDescendantToPaths< + TRouter, + RemoveTrailingSlashes + > extends never + ? never + : `${RemoveTrailingSlashes}/${ParentPath}` + : never) + +export type RelativeToCurrentPath< + TRouter extends AnyRouter, + TFrom extends string, + TTo extends string, + TResolvedPath extends string = ResolveRelativePath, +> = RelativeToPath | CurrentPath + +export type AbsoluteToPath = + | (string extends TFrom + ? CurrentPath + : TFrom extends `/` + ? never + : CurrentPath) + | (string extends TFrom + ? ParentPath + : TFrom extends `/` + ? never + : ParentPath) + | RouteToPath + | (TFrom extends '/' + ? never + : string extends TFrom + ? never + : InferDescendantToPaths>) + +export type RelativeToPathAutoComplete< + TRouter extends AnyRouter, + TFrom extends string, + TTo extends string, +> = string extends TTo + ? string + : string extends TFrom + ? AbsoluteToPath + : TTo & `..${string}` extends never + ? TTo & `.${string}` extends never + ? AbsoluteToPath + : RelativeToCurrentPath + : RelativeToParentPath + +export type NavigateOptions< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends string = string, + TTo extends string | undefined = '.', + TMaskFrom extends string = TFrom, + TMaskTo extends string = '.', +> = ToOptions & NavigateOptionProps + +export interface NavigateOptionProps { + // if set to `true`, the router will scroll the element with an id matching the hash into view with default ScrollIntoViewOptions. + // if set to `false`, the router will not scroll the element with an id matching the hash into view. + // if set to `ScrollIntoViewOptions`, the router will scroll the element with an id matching the hash into view with the provided options. + hashScrollIntoView?: boolean | ScrollIntoViewOptions + // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one. + replace?: boolean + resetScroll?: boolean + /** @deprecated All navigations now use startTransition under the hood */ + startTransition?: boolean + // if set to `true`, the router will wrap the resulting navigation in a document.startViewTransition() call. + // if set to `ViewTransitionOptions`, the router will pass the `types` field to document.startViewTransition({update: fn, types: viewTransition.types}) call + viewTransition?: boolean | ViewTransitionOptions + ignoreBlocker?: boolean + reloadDocument?: boolean + href?: string +} + +export type ToOptions< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends string = string, + TTo extends string | undefined = '.', + TMaskFrom extends string = TFrom, + TMaskTo extends string = '.', +> = ToSubOptions & MaskOptions + +export interface MaskOptions< + in out TRouter extends AnyRouter, + in out TMaskFrom extends string, + in out TMaskTo extends string, +> { + _fromLocation?: ParsedLocation + mask?: ToMaskOptions +} + +export type ToMaskOptions< + TRouter extends AnyRouter = RegisteredRouter, + TMaskFrom extends string = string, + TMaskTo extends string = '.', +> = ToSubOptions & { + unmaskOnReload?: boolean +} + +export type ToSubOptions< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends string = string, + TTo extends string | undefined = '.', +> = ToSubOptionsProps & + SearchParamOptions & + PathParamOptions + +export interface RequiredToOptions< + in out TRouter extends AnyRouter, + in out TFrom extends string, + in out TTo extends string | undefined, +> { + to: ToPathOption & {} +} + +export interface OptionalToOptions< + in out TRouter extends AnyRouter, + in out TFrom extends string, + in out TTo extends string | undefined, +> { + to?: ToPathOption & {} +} + +export type MakeToRequired< + TRouter extends AnyRouter, + TFrom extends string, + TTo extends string | undefined, +> = string extends TFrom + ? string extends TTo + ? OptionalToOptions + : TTo & CatchAllPaths extends never + ? RequiredToOptions + : OptionalToOptions + : OptionalToOptions + +export type ToSubOptionsProps< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends RoutePaths | string = string, + TTo extends string | undefined = '.', +> = MakeToRequired & { + hash?: true | Updater + state?: true | NonNullableUpdater + from?: FromPathOption & {} +} + +export type ParamsReducerFn< + in out TRouter extends AnyRouter, + in out TParamVariant extends ParamVariant, + in out TFrom, + in out TTo, +> = ( + current: Expand>, +) => Expand> + +type ParamsReducer< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, + TFrom, + TTo, +> = + | Expand> + | (ParamsReducerFn & {}) + +type ParamVariant = 'PATH' | 'SEARCH' + +export type ResolveRoute< + TRouter extends AnyRouter, + TFrom, + TTo, + TPath = ResolveRelativePath, +> = TPath extends string + ? TFrom extends TPath + ? RouteByPath + : RouteByToPath + : never + +type ResolveFromParamType = + TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchema' + +type ResolveFromAllParams< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, +> = TParamVariant extends 'PATH' + ? AllParams + : FullSearchSchema + +type ResolveFromParams< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, + TFrom, +> = string extends TFrom + ? ResolveFromAllParams + : RouteByPath< + TRouter['routeTree'], + TFrom + >['types'][ResolveFromParamType] + +type ResolveToParamType = + TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchemaInput' + +type ResolveAllToParams< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, +> = TParamVariant extends 'PATH' + ? AllParams + : FullSearchSchemaInput + +export type ResolveToParams< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, + TFrom, + TTo, +> = + ResolveRelativePath extends infer TPath + ? undefined extends TPath + ? never + : string extends TPath + ? ResolveAllToParams + : TPath extends CatchAllPaths + ? ResolveAllToParams + : ResolveRoute< + TRouter, + TFrom, + TTo + >['types'][ResolveToParamType] + : never + +type ResolveRelativeToParams< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, + TFrom, + TTo, + TToParams = ResolveToParams, +> = TParamVariant extends 'SEARCH' + ? TToParams + : string extends TFrom + ? TToParams + : MakeDifferenceOptional< + ResolveFromParams, + TToParams + > + +export interface MakeOptionalSearchParams< + in out TRouter extends AnyRouter, + in out TFrom, + in out TTo, +> { + search?: true | (ParamsReducer & {}) +} + +export interface MakeOptionalPathParams< + in out TRouter extends AnyRouter, + in out TFrom, + in out TTo, +> { + params?: true | (ParamsReducer & {}) +} + +type MakeRequiredParamsReducer< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, + TFrom, + TTo, +> = + | (string extends TFrom + ? never + : ResolveFromParams extends WithoutEmpty< + PickRequired< + ResolveRelativeToParams + > + > + ? true + : never) + | (ParamsReducer & {}) + +export interface MakeRequiredPathParams< + in out TRouter extends AnyRouter, + in out TFrom, + in out TTo, +> { + params: MakeRequiredParamsReducer & {} +} + +export interface MakeRequiredSearchParams< + in out TRouter extends AnyRouter, + in out TFrom, + in out TTo, +> { + search: MakeRequiredParamsReducer & {} +} + +export type IsRequired< + TRouter extends AnyRouter, + TParamVariant extends ParamVariant, + TFrom, + TTo, +> = + ResolveRelativePath extends infer TPath + ? undefined extends TPath + ? never + : TPath extends CatchAllPaths + ? never + : IsRequiredParams< + ResolveRelativeToParams + > + : never + +export type SearchParamOptions = + IsRequired extends never + ? MakeOptionalSearchParams + : MakeRequiredSearchParams + +export type PathParamOptions = + IsRequired extends never + ? MakeOptionalPathParams + : MakeRequiredPathParams + +export type ToPathOption< + TRouter extends AnyRouter = AnyRouter, + TFrom extends string = string, + TTo extends string | undefined = string, +> = ConstrainLiteral< + TTo, + RelativeToPathAutoComplete< + TRouter, + NoInfer extends string ? NoInfer : '', + NoInfer & string + > +> + +export type FromPathOption = ConstrainLiteral< + TFrom, + RoutePaths +> + +export interface ActiveOptions { + exact?: boolean + includeHash?: boolean + includeSearch?: boolean + explicitUndefined?: boolean +} + +export interface LinkOptionsProps { + /** + * The standard anchor tag target attribute + */ + target?: HTMLAnchorElement['target'] + /** + * Configurable options to determine if the link should be considered active or not + * @default {exact:true,includeHash:true} + */ + activeOptions?: ActiveOptions + /** + * The preloading strategy for this link + * - `false` - No preloading + * - `'intent'` - Preload the linked route on hover and cache it for this many milliseconds in hopes that the user will eventually navigate there. + * - `'viewport'` - Preload the linked route when it enters the viewport + */ + preload?: false | 'intent' | 'viewport' | 'render' + /** + * When a preload strategy is set, this delays the preload by this many milliseconds. + * If the user exits the link before this delay, the preload will be cancelled. + */ + preloadDelay?: number + /** + * Control whether the link should be disabled or not + * If set to `true`, the link will be rendered without an `href` attribute + * @default false + */ + disabled?: boolean +} + +export type LinkOptions< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends string = string, + TTo extends string | undefined = '.', + TMaskFrom extends string = TFrom, + TMaskTo extends string = '.', +> = NavigateOptions & LinkOptionsProps + export type LinkCurrentTargetElement = { preloadTimeout?: null | ReturnType } diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index 5dffd6b68ec..d7345403c0e 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1,11 +1,15 @@ import type { ParsePathParams } from './link' import type { RootRouteId } from './root' -import type { Assign } from './utils' +import type { ParseRoute } from './routeInfo' +import type { RegisteredRouter } from './router' +import type { Assign, Expand, IntersectAssign } from './utils' import type { AnySchema, AnyStandardSchemaValidator, AnyValidatorAdapter, AnyValidatorObj, + ResolveSearchValidatorInput, + ResolveValidatorOutput, StandardSchemaValidator, ValidatorAdapter, ValidatorFn, @@ -213,6 +217,38 @@ export type DefaultSearchValidator = SearchValidator< AnySchema > +export type RoutePrefix< + TPrefix extends string, + TPath extends string, +> = string extends TPath + ? RootRouteId + : TPath extends string + ? TPrefix extends RootRouteId + ? TPath extends '/' + ? '/' + : `/${TrimPath}` + : `${TPrefix}/${TPath}` extends '/' + ? '/' + : `/${TrimPathLeft<`${TrimPathRight}/${TrimPath}`>}` + : never + +export type TrimPath = '' extends T + ? '' + : TrimPathRight> + +export type TrimPathLeft = + T extends `${RootRouteId}/${infer U}` + ? TrimPathLeft + : T extends `/${infer U}` + ? TrimPathLeft + : T + +export type TrimPathRight = T extends '/' + ? '/' + : T extends `${infer U}/` + ? TrimPathRight + : T + export type LooseReturnType = T extends ( ...args: Array ) => infer TReturn @@ -250,36 +286,222 @@ export type ResolveLoaderData = unknown extends TLoaderFn ? undefined : LooseAsyncReturnType -export type RoutePrefix< - TPrefix extends string, +export type ResolveFullSearchSchema< + TParentRoute extends AnyRoute, + TSearchValidator, +> = unknown extends TParentRoute + ? ResolveValidatorOutput + : IntersectAssign< + InferFullSearchSchema, + ResolveValidatorOutput + > + +export type ResolveFullSearchSchemaInput< + TParentRoute extends AnyRoute, + TSearchValidator, +> = IntersectAssign< + InferFullSearchSchemaInput, + ResolveSearchValidatorInput +> + +export type ResolveAllParamsFromParent< + TParentRoute extends AnyRoute, + TParams, +> = Assign, TParams> + +export type RouteContextParameter< + TParentRoute extends AnyRoute, + TRouterContext, +> = unknown extends TParentRoute + ? TRouterContext + : Assign> + +export type BeforeLoadContextParameter< + TParentRoute extends AnyRoute, + TRouterContext, + TRouteContextFn, +> = Assign< + RouteContextParameter, + ContextReturnType +> + +export type ResolveAllContext< + TParentRoute extends AnyRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, +> = Assign< + BeforeLoadContextParameter, + ContextAsyncReturnType +> +export interface FullSearchSchemaOption< + in out TParentRoute extends AnyRoute, + in out TSearchValidator, +> { + search: Expand> +} + +export interface RemountDepsOptions< + in out TRouteId, + in out TFullSearchSchema, + in out TAllParams, + in out TLoaderDeps, +> { + routeId: TRouteId + search: TFullSearchSchema + params: TAllParams + loaderDeps: TLoaderDeps +} + +export type MakeRemountDepsOptionsUnion< + TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], + TRoute extends AnyRoute = ParseRoute, +> = TRoute extends any + ? RemountDepsOptions< + TRoute['id'], + TRoute['types']['fullSearchSchema'], + TRoute['types']['allParams'], + TRoute['types']['loaderDeps'] + > + : never + +export interface RouteTypes< + in out TParentRoute extends AnyRoute, + in out TPath extends string, + in out TFullPath extends string, + in out TCustomId extends string, + in out TId extends string, + in out TSearchValidator, + in out TParams, + in out TRouterContext, + in out TRouteContextFn, + in out TBeforeLoadFn, + in out TLoaderDeps, + in out TLoaderFn, + in out TChildren, + in out TFileRouteTypes, +> { + parentRoute: TParentRoute + path: TPath + to: TrimPathRight + fullPath: TFullPath + customId: TCustomId + id: TId + searchSchema: ResolveValidatorOutput + searchSchemaInput: ResolveSearchValidatorInput + searchValidator: TSearchValidator + fullSearchSchema: ResolveFullSearchSchema + fullSearchSchemaInput: ResolveFullSearchSchemaInput< + TParentRoute, + TSearchValidator + > + params: TParams + allParams: ResolveAllParamsFromParent + routerContext: TRouterContext + routeContext: ResolveRouteContext + routeContextFn: TRouteContextFn + beforeLoadFn: TBeforeLoadFn + allContext: ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + > + children: TChildren + loaderData: ResolveLoaderData + loaderDeps: TLoaderDeps + fileRouteTypes: TFileRouteTypes +} + +export type ResolveFullPath< + TParentRoute extends AnyRoute, TPath extends string, -> = string extends TPath - ? RootRouteId - : TPath extends string - ? TPrefix extends RootRouteId - ? TPath extends '/' - ? '/' - : `/${TrimPath}` - : `${TPrefix}/${TPath}` extends '/' - ? '/' - : `/${TrimPathLeft<`${TrimPathRight}/${TrimPath}`>}` - : never + TPrefixed = RoutePrefix, +> = TPrefixed extends RootRouteId ? '/' : TPrefixed + +export interface Route< + in out TParentRoute extends AnyRoute, + in out TPath extends string, + in out TFullPath extends string, + in out TCustomId extends string, + in out TId extends string, + in out TSearchValidator, + in out TParams, + in out TRouterContext, + in out TRouteContextFn, + in out TBeforeLoadFn, + in out TLoaderDeps, + in out TLoaderFn, + in out TChildren, + in out TFileRouteTypes, +> { + fullPath: TFullPath + path: TPath + id: TId + types: RouteTypes< + TParentRoute, + TPath, + TFullPath, + TCustomId, + TId, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > +} -export type TrimPath = '' extends T - ? '' - : TrimPathRight> +export type AnyRoute = Route< + any, + any, + any, + any, + any, + any, + any, + any, + any, + any, + any, + any, + any, + any +> -export type TrimPathLeft = - T extends `${RootRouteId}/${infer U}` - ? TrimPathLeft - : T extends `/${infer U}` - ? TrimPathLeft - : T -export type TrimPathRight = T extends '/' - ? '/' - : T extends `${infer U}/` - ? TrimPathRight - : T +export interface RootRoute< + in out TSearchValidator, + in out TRouterContext, + in out TRouteContextFn, + in out TBeforeLoadFn, + in out TLoaderDeps extends Record, + in out TLoaderFn, + in out TChildren, + in out TFileRouteTypes, +> extends Route< + any, // TParentRoute + '/', // TPath + '/', // TFullPath + string, // TCustomId + RootRouteId, // TId + TSearchValidator, // TSearchValidator + {}, // TParams + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, // TChildren + TFileRouteTypes + > {} + +export type AnyRouteWithContext = AnyRoute & { + types: { allContext: TContext } +} /** * @deprecated Use `ErrorComponentProps` instead. diff --git a/packages/react-router/src/routeInfo.ts b/packages/router-core/src/routeInfo.ts similarity index 97% rename from packages/react-router/src/routeInfo.ts rename to packages/router-core/src/routeInfo.ts index 45bb5a30d33..6c9249e091a 100644 --- a/packages/react-router/src/routeInfo.ts +++ b/packages/router-core/src/routeInfo.ts @@ -1,12 +1,8 @@ import type { InferFileRouteTypes } from './fileRoute' -import type { - AddTrailingSlash, - PartialMergeAll, - RemoveTrailingSlashes, - TrailingSlashOption, -} from '@tanstack/router-core' +import type { AddTrailingSlash, RemoveTrailingSlashes } from './link' import type { AnyRoute } from './route' -import type { AnyRouter } from './router' +import type { AnyRouter, TrailingSlashOption } from './router' +import type { PartialMergeAll } from './utils' export type ParseRoute = TRouteTree extends { types: { children: infer TChildren } diff --git a/packages/router-core/src/router.ts b/packages/router-core/src/router.ts index eb6af1b6b43..abebbdd304f 100644 --- a/packages/router-core/src/router.ts +++ b/packages/router-core/src/router.ts @@ -1,6 +1,38 @@ import type { ParsedLocation } from './location' import type { DeferredPromiseState } from './defer' import type { ControlledPromise } from './utils' +import type { AnyRoute, AnyRouteWithContext } from './route' + +export interface Register { + // router: Router +} + +export type RegisteredRouter = Register extends { + router: infer TRouter extends AnyRouter +} + ? TRouter + : AnyRouter + +export interface RouterOptions< + in out TTrailingSlashOption extends TrailingSlashOption, +> { + trailingSlash?: TTrailingSlashOption +} + +export interface Router< + in out TRouteTree extends AnyRoute, + in out TTrailingSlashOption extends TrailingSlashOption, +> { + routeTree: TRouteTree + options: RouterOptions +} + +export type AnyRouterWithContext = Router< + AnyRouteWithContext, + any +> + +export type AnyRouter = Router export interface ViewTransitionOptions { types: Array diff --git a/packages/solid-router/src/Matches.tsx b/packages/solid-router/src/Matches.tsx index cc68f8bc96d..f9fdc8b31b9 100644 --- a/packages/solid-router/src/Matches.tsx +++ b/packages/solid-router/src/Matches.tsx @@ -9,30 +9,26 @@ import { Match } from './Match' import { SafeFragment } from './SafeFragment' import type { AnyRoute } from './route' import type { + AllContext, + AllLoaderData, + AllParams, ControlledPromise, DeepPartial, - NoInfer, - ResolveRelativePath, - StaticDataRouteOption, -} from '@tanstack/router-core' -import type { AnyRouter, RegisteredRouter, RouterState } from './router' -import type { + FullSearchSchema, MakeOptionalPathParams, MakeOptionalSearchParams, MaskOptions, - ResolveRoute, - ToSubOptionsProps, -} from './link' -import type { - AllContext, - AllLoaderData, - AllParams, - FullSearchSchema, + NoInfer, ParseRoute, + ResolveRelativePath, + ResolveRoute, RouteById, RouteByPath, RouteIds, -} from './routeInfo' + StaticDataRouteOption, + ToSubOptionsProps, +} from '@tanstack/router-core' +import type { AnyRouter, RegisteredRouter, RouterState } from './router' export type MakeRouteMatchFromRoute = RouteMatch< TRoute['types']['id'], diff --git a/packages/solid-router/src/RouterProvider.tsx b/packages/solid-router/src/RouterProvider.tsx index 453df099d7f..75104eeee44 100644 --- a/packages/solid-router/src/RouterProvider.tsx +++ b/packages/solid-router/src/RouterProvider.tsx @@ -1,12 +1,6 @@ import { Matches } from './Matches' import { getRouterContext } from './routerContext' import type * as Solid from 'solid-js' -import type { NavigateOptions, ToOptions } from './link' -import type { - ParsedLocation, - ViewTransitionOptions, -} from '@tanstack/router-core' -import type { RoutePaths } from './routeInfo' import type { AnyRouter, RegisteredRouter, @@ -14,48 +8,6 @@ import type { RouterOptions, } from './router' -export interface CommitLocationOptions { - replace?: boolean - resetScroll?: boolean - hashScrollIntoView?: boolean | ScrollIntoViewOptions - viewTransition?: boolean | ViewTransitionOptions - /** - * @deprecated All navigations use Solid transitions under the hood now - **/ - startTransition?: boolean - ignoreBlocker?: boolean -} - -export interface MatchLocation { - to?: string | number | null - fuzzy?: boolean - caseSensitive?: boolean - from?: string -} - -export type NavigateFn = < - TRouter extends RegisteredRouter, - TTo extends string | undefined, - TFrom extends RoutePaths | string = string, - TMaskFrom extends RoutePaths | string = TFrom, - TMaskTo extends string = '', ->( - opts: NavigateOptions, -) => Promise | void - -export type BuildLocationFn = < - TRouter extends RegisteredRouter, - TTo extends string | undefined, - TFrom extends RoutePaths | string = string, - TMaskFrom extends RoutePaths | string = TFrom, - TMaskTo extends string = '', ->( - opts: ToOptions & { - leaveParams?: boolean - _includeValidateSearch?: boolean - }, -) => ParsedLocation - export function RouterContextProvider< TRouter extends AnyRouter = RegisteredRouter, TDehydrated extends Record = Record, diff --git a/packages/solid-router/src/fileRoute.ts b/packages/solid-router/src/fileRoute.ts index 08a173b6dd5..34a3031b4a3 100644 --- a/packages/solid-router/src/fileRoute.ts +++ b/packages/solid-router/src/fileRoute.ts @@ -15,53 +15,25 @@ import type { AnyPathParams, AnyValidator, Constrain, + FileRoutesByPath, ResolveParams, + RouteById, + RouteIds, } from '@tanstack/router-core' import type { AnyRoute, FileBaseRouteOptions, - RootRoute, Route, RouteConstraints, RouteLoaderFn, UpdatableRouteOptions, } from './route' import type { RegisteredRouter } from './router' -import type { RouteById, RouteIds } from './routeInfo' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' import type { UseRouteContextRoute } from './useRouteContext' -export interface FileRoutesByPath { - // '/': { - // parentRoute: typeof rootRoute - // } -} - -export interface FileRouteTypes { - fileRoutesByFullPath: any - fullPaths: any - to: any - fileRoutesByTo: any - id: any - fileRoutesById: any -} - -export type InferFileRouteTypes = - TRouteTree extends RootRoute< - any, - any, - any, - any, - any, - any, - any, - infer TFileRouteTypes extends FileRouteTypes - > - ? TFileRouteTypes - : never - export function createFileRoute< TFilePath extends keyof FileRoutesByPath, TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'], diff --git a/packages/solid-router/src/index.tsx b/packages/solid-router/src/index.tsx index 1c0a4e6d01b..c40bae7d81a 100644 --- a/packages/solid-router/src/index.tsx +++ b/packages/solid-router/src/index.tsx @@ -120,6 +120,51 @@ export type { Validator, ValidatorAdapter, ValidatorObj, + NavigateFn, + BuildLocationFn, + InferDescendantToPaths, + RelativeToPath, + RelativeToParentPath, + RelativeToCurrentPath, + Register, + AbsoluteToPath, + RelativeToPathAutoComplete, + NavigateOptions, + ToOptions, + ToMaskOptions, + ToSubOptions, + ResolveRoute, + SearchParamOptions, + PathParamOptions, + ToPathOption, + LinkOptions, + MakeOptionalPathParams, + AnyRouterWithContext, + ParseRoute, + RoutesById, + RouteById, + RouteIds, + RoutesByPath, + RouteByPath, + RoutePaths, + FullSearchSchema, + AllParams, + AllLoaderData, + FullSearchSchemaInput, + AllContext, + CommitLocationOptions, + MatchLocation, + ResolveFullSearchSchema, + ResolveFullSearchSchemaInput, + ResolveAllParamsFromParent, + RouteContextParameter, + BeforeLoadContextParameter, + ResolveAllContext, + FullSearchSchemaOption, + MakeRemountDepsOptionsUnion, + RemountDepsOptions, + FileRouteTypes, + FileRoutesByPath, } from '@tanstack/router-core' export { @@ -150,11 +195,7 @@ export { createLazyRoute, createLazyFileRoute, } from './fileRoute' -export type { - FileRoutesByPath, - FileRouteTypes, - LazyRouteOptions, -} from './fileRoute' +export type { LazyRouteOptions } from './fileRoute' export * from './history' @@ -162,28 +203,12 @@ export { lazyRouteComponent } from './lazyRouteComponent' export { useLinkProps, createLink, Link, linkOptions } from './link' export type { - InferDescendantToPaths, - RelativeToPath, - RelativeToParentPath, - RelativeToCurrentPath, - AbsoluteToPath, - RelativeToPathAutoComplete, - NavigateOptions, - ToOptions, - ToMaskOptions, - ToSubOptions, - ResolveRoute, - SearchParamOptions, - PathParamOptions, - ToPathOption, - LinkOptions, UseLinkPropsOptions, ActiveLinkOptions, LinkProps, LinkComponent, LinkComponentProps, CreateLinkProps, - MakeOptionalPathParams, } from './link' export { @@ -234,8 +259,6 @@ export type { UpdatableRouteOptions, RouteLoaderFn, LoaderFnContext, - ResolveFullSearchSchema, - ResolveFullSearchSchemaInput, AnyRoute, RouteConstraints, AnyRootRoute, @@ -249,35 +272,13 @@ export type { NotFoundRouteComponent, RootRouteOptions, AnyRouteWithContext, - FullSearchSchemaOption, RouteContextFn, RouteContextOptions, BeforeLoadFn, BeforeLoadContextOptions, ContextOptions, - RouteContextParameter, - BeforeLoadContextParameter, - ResolveAllContext, - ResolveAllParamsFromParent, - MakeRemountDepsOptionsUnion, - RemountDepsOptions, } from './route' -export type { - ParseRoute, - RoutesById, - RouteById, - RouteIds, - RoutesByPath, - RouteByPath, - RoutePaths, - FullSearchSchema, - AllParams, - AllLoaderData, - FullSearchSchemaInput, - AllContext, -} from './routeInfo' - export { componentTypes, createRouter, @@ -289,7 +290,6 @@ export { } from './router' export type { - Register, AnyRouter, RegisteredRouter, RouterContextOptions, @@ -302,19 +302,12 @@ export type { RouterEvents, RouterEvent, RouterListener, - AnyRouterWithContext, ControllablePromise, InjectedHtmlEntry, } from './router' export { RouterProvider, RouterContextProvider } from './RouterProvider' -export type { - RouterProps, - CommitLocationOptions, - MatchLocation, - NavigateFn, - BuildLocationFn, -} from './RouterProvider' +export type { RouterProps } from './RouterProvider' export { useElementScrollRestoration, diff --git a/packages/solid-router/src/link.tsx b/packages/solid-router/src/link.tsx index 67344fea5f2..4afeb5b9400 100644 --- a/packages/solid-router/src/link.tsx +++ b/packages/solid-router/src/link.tsx @@ -18,440 +18,16 @@ import { useIntersectionObserver } from './utils' import { useMatch } from './useMatch' import type { Constrain, - ConstrainLiteral, - Expand, - IsRequiredParams, - LinkOptionsProps, - MakeDifferenceOptional, - NoInfer, - NonNullableUpdater, - ParsedLocation, - PickRequired, - RemoveTrailingSlashes, - ResolveRelativePath, - Updater, - ViewTransitionOptions, - WithoutEmpty, -} from '@tanstack/router-core' -import type { HistoryState, ParsedHistoryState } from '@tanstack/history' -import type { - AllParams, - CatchAllPaths, - CurrentPath, - FullSearchSchema, - FullSearchSchemaInput, - ParentPath, - RouteByPath, - RouteByToPath, + LinkCurrentTargetElement, + LinkOptions, RoutePaths, - RouteToPath, - ToPath, -} from './routeInfo' +} from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' import type { ValidateLinkOptions, ValidateLinkOptionsArray, } from './typePrimitives' -export type FindDescendantToPaths< - TRouter extends AnyRouter, - TPrefix extends string, -> = `${TPrefix}/${string}` & RouteToPath - -export type InferDescendantToPaths< - TRouter extends AnyRouter, - TPrefix extends string, - TPaths = FindDescendantToPaths, -> = TPaths extends `${TPrefix}/` - ? never - : TPaths extends `${TPrefix}/${infer TRest}` - ? TRest - : never - -export type RelativeToPath< - TRouter extends AnyRouter, - TTo extends string, - TResolvedPath extends string, -> = - | (TResolvedPath & RouteToPath extends never - ? never - : ToPath) - | `${RemoveTrailingSlashes}/${InferDescendantToPaths>}` - -export type RelativeToParentPath< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string, - TResolvedPath extends string = ResolveRelativePath, -> = - | RelativeToPath - | (TTo extends `${string}..` | `${string}../` - ? TResolvedPath extends '/' | '' - ? never - : FindDescendantToPaths< - TRouter, - RemoveTrailingSlashes - > extends never - ? never - : `${RemoveTrailingSlashes}/${ParentPath}` - : never) - -export type RelativeToCurrentPath< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string, - TResolvedPath extends string = ResolveRelativePath, -> = RelativeToPath | CurrentPath - -export type AbsoluteToPath = - | (string extends TFrom - ? CurrentPath - : TFrom extends `/` - ? never - : CurrentPath) - | (string extends TFrom - ? ParentPath - : TFrom extends `/` - ? never - : ParentPath) - | RouteToPath - | (TFrom extends '/' - ? never - : string extends TFrom - ? never - : InferDescendantToPaths>) - -export type RelativeToPathAutoComplete< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string, -> = string extends TTo - ? string - : string extends TFrom - ? AbsoluteToPath - : TTo & `..${string}` extends never - ? TTo & `.${string}` extends never - ? AbsoluteToPath - : RelativeToCurrentPath - : RelativeToParentPath - -export type NavigateOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', - TMaskFrom extends string = TFrom, - TMaskTo extends string = '.', -> = ToOptions & NavigateOptionProps - -export interface NavigateOptionProps { - // if set to `true`, the router will scroll the element with an id matching the hash into view with default ScrollIntoViewOptions. - // if set to `false`, the router will not scroll the element with an id matching the hash into view. - // if set to `ScrollIntoViewOptions`, the router will scroll the element with an id matching the hash into view with the provided options. - hashScrollIntoView?: boolean | ScrollIntoViewOptions - // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one. - replace?: boolean - resetScroll?: boolean - /** @deprecated All navigations now use startTransition under the hood */ - startTransition?: boolean - // if set to `true`, the router will wrap the resulting navigation in a document.startViewTransition() call. - // if set to `ViewTransitionOptions`, the router will pass the `types` field to document.startViewTransition({update: fn, types: viewTransition.types}) call - viewTransition?: boolean | ViewTransitionOptions - ignoreBlocker?: boolean - reloadDocument?: boolean - href?: string -} - -export type ToOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', - TMaskFrom extends string = TFrom, - TMaskTo extends string = '.', -> = ToSubOptions & MaskOptions - -export interface MaskOptions< - in out TRouter extends AnyRouter, - in out TMaskFrom extends string, - in out TMaskTo extends string, -> { - _fromLocation?: ParsedLocation - mask?: ToMaskOptions -} - -export type ToMaskOptions< - TRouter extends AnyRouter = RegisteredRouter, - TMaskFrom extends string = string, - TMaskTo extends string = '.', -> = ToSubOptions & { - unmaskOnReload?: boolean -} - -export type ToSubOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', -> = ToSubOptionsProps & - SearchParamOptions & - PathParamOptions - -export interface RequiredToOptions< - in out TRouter extends AnyRouter, - in out TFrom extends string, - in out TTo extends string | undefined, -> { - to: ToPathOption & {} -} - -export interface OptionalToOptions< - in out TRouter extends AnyRouter, - in out TFrom extends string, - in out TTo extends string | undefined, -> { - to?: ToPathOption & {} -} - -export type MakeToRequired< - TRouter extends AnyRouter, - TFrom extends string, - TTo extends string | undefined, -> = string extends TFrom - ? string extends TTo - ? OptionalToOptions - : TTo & CatchAllPaths extends never - ? RequiredToOptions - : OptionalToOptions - : OptionalToOptions - -export type ToSubOptionsProps< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends RoutePaths | string = string, - TTo extends string | undefined = '.', -> = MakeToRequired & { - hash?: true | Updater - state?: true | NonNullableUpdater - from?: FromPathOption & {} -} - -export type ParamsReducerFn< - in out TRouter extends AnyRouter, - in out TParamVariant extends ParamVariant, - in out TFrom, - in out TTo, -> = ( - current: Expand>, -) => Expand> - -type ParamsReducer< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - | Expand> - | (ParamsReducerFn & {}) - -type ParamVariant = 'PATH' | 'SEARCH' - -export type ResolveRoute< - TRouter extends AnyRouter, - TFrom, - TTo, - TPath = ResolveRelativePath, -> = TPath extends string - ? TFrom extends TPath - ? RouteByPath - : RouteByToPath - : never - -type ResolveFromParamType = - TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchema' - -type ResolveFromAllParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, -> = TParamVariant extends 'PATH' - ? AllParams - : FullSearchSchema - -type ResolveFromParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, -> = string extends TFrom - ? ResolveFromAllParams - : RouteByPath< - TRouter['routeTree'], - TFrom - >['types'][ResolveFromParamType] - -type ResolveToParamType = - TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchemaInput' - -type ResolveAllToParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, -> = TParamVariant extends 'PATH' - ? AllParams - : FullSearchSchemaInput - -export type ResolveToParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - ResolveRelativePath extends infer TPath - ? undefined extends TPath - ? never - : string extends TPath - ? ResolveAllToParams - : TPath extends CatchAllPaths - ? ResolveAllToParams - : ResolveRoute< - TRouter, - TFrom, - TTo - >['types'][ResolveToParamType] - : never - -type ResolveRelativeToParams< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, - TToParams = ResolveToParams, -> = TParamVariant extends 'SEARCH' - ? TToParams - : string extends TFrom - ? TToParams - : MakeDifferenceOptional< - ResolveFromParams, - TToParams - > - -export interface MakeOptionalSearchParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - search?: true | (ParamsReducer & {}) -} - -export interface MakeOptionalPathParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - params?: true | (ParamsReducer & {}) -} - -type MakeRequiredParamsReducer< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - | (string extends TFrom - ? never - : ResolveFromParams extends WithoutEmpty< - PickRequired< - ResolveRelativeToParams - > - > - ? true - : never) - | (ParamsReducer & {}) - -export interface MakeRequiredPathParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - params: MakeRequiredParamsReducer & {} -} - -export interface MakeRequiredSearchParams< - in out TRouter extends AnyRouter, - in out TFrom, - in out TTo, -> { - search: MakeRequiredParamsReducer & {} -} - -export type IsRequired< - TRouter extends AnyRouter, - TParamVariant extends ParamVariant, - TFrom, - TTo, -> = - ResolveRelativePath extends infer TPath - ? undefined extends TPath - ? never - : TPath extends CatchAllPaths - ? never - : IsRequiredParams< - ResolveRelativeToParams - > - : never - -export type SearchParamOptions = - IsRequired extends never - ? MakeOptionalSearchParams - : MakeRequiredSearchParams - -export type PathParamOptions = - IsRequired extends never - ? MakeOptionalPathParams - : MakeRequiredPathParams - -export type ToPathOption< - TRouter extends AnyRouter = AnyRouter, - TFrom extends string = string, - TTo extends string | undefined = string, -> = ConstrainLiteral< - TTo, - RelativeToPathAutoComplete< - TRouter, - NoInfer extends string ? NoInfer : '', - NoInfer & string - > -> - -export type FromPathOption = ConstrainLiteral< - TFrom, - RoutePaths -> - -export type LinkOptions< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends string = string, - TTo extends string | undefined = '.', - TMaskFrom extends string = TFrom, - TMaskTo extends string = '.', -> = NavigateOptions & LinkOptionsProps - -// type Test1 = ResolveRelativePath<'/', '/posts'> -// // ^? -// type Test4 = ResolveRelativePath<'/posts/1/comments', '../..'> -// // ^? -// type Test5 = ResolveRelativePath<'/posts/1/comments', '../../..'> -// // ^? -// type Test6 = ResolveRelativePath<'/posts/1/comments', './1'> -// // ^? -// type Test7 = ResolveRelativePath<'/posts/1/comments', './1/2'> -// // ^? -// type Test8 = ResolveRelativePath<'/posts/1/comments', '../edit'> -// // ^? -// type Test9 = ResolveRelativePath<'/posts/1/comments', '1'> -// // ^? -// type Test10 = ResolveRelativePath<'/posts/1/comments', './1'> -// // ^? -// type Test11 = ResolveRelativePath<'/posts/1/comments', './1/2'> -// // ^? -type LinkCurrentTargetElement = { - preloadTimeout?: null | ReturnType -} - export function useLinkProps< TRouter extends AnyRouter = RegisteredRouter, TFrom extends RoutePaths | string = string, diff --git a/packages/solid-router/src/not-found.tsx b/packages/solid-router/src/not-found.tsx index 0906c5fadbb..ef0511adf21 100644 --- a/packages/solid-router/src/not-found.tsx +++ b/packages/solid-router/src/not-found.tsx @@ -2,7 +2,7 @@ import { CatchBoundary } from './CatchBoundary' import { useRouterState } from './useRouterState' import type * as Solid from 'solid-js' import type { RegisteredRouter } from './router' -import type { RouteIds } from './routeInfo' +import type { RouteIds } from '@tanstack/router-core' export type NotFoundError = { /** diff --git a/packages/solid-router/src/redirects.ts b/packages/solid-router/src/redirects.ts index 9f90c2434c8..348d7aa40d6 100644 --- a/packages/solid-router/src/redirects.ts +++ b/packages/solid-router/src/redirects.ts @@ -1,7 +1,9 @@ -import type { NavigateOptions } from './link' -import type { RoutePaths } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' -import type { PickAsRequired } from '@tanstack/router-core' +import type { + NavigateOptions, + PickAsRequired, + RoutePaths, +} from '@tanstack/router-core' export type AnyRedirect = Redirect diff --git a/packages/solid-router/src/route.ts b/packages/solid-router/src/route.ts index 77685b2c42f..131021c0579 100644 --- a/packages/solid-router/src/route.ts +++ b/packages/solid-router/src/route.ts @@ -12,36 +12,43 @@ import type { AnyPathParams, AnySchema, AnyValidator, - Assign, + BeforeLoadContextParameter, + BuildLocationFn, Constrain, ConstrainLiteral, - ContextAsyncReturnType, - ContextReturnType, + RootRoute as CoreRootRoute, + Route as CoreRoute, DefaultValidator, ErrorComponentProps, Expand, - InferAllContext, - InferAllParams, - InferFullSearchSchema, - InferFullSearchSchemaInput, - IntersectAssign, + FullSearchSchemaOption, + NavigateFn, + NavigateOptions, NoInfer, NotFoundRouteProps, ParamsOptions, ParsedLocation, + RemountDepsOptions, + ResolveAllContext, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveFullSearchSchemaInput, ResolveId, ResolveLoaderData, ResolveParams, - ResolveRouteContext, - ResolveSearchValidatorInput, - ResolveValidatorOutput, RootRouteId, + RouteById, RouteContext, + RouteContextParameter, + RouteIds, RoutePathOptions, RoutePathOptionsIntersection, + RoutePaths, RoutePrefix, + RouteTypes, SearchFilter, SearchMiddleware, + ToMaskOptions, TrimPathRight, UpdatableStaticRouteOption, } from '@tanstack/router-core' @@ -58,10 +65,7 @@ import type { MakeRouteMatchUnion, RouteMatch, } from './Matches' -import type { NavigateOptions, ToMaskOptions } from './link' -import type { ParseRoute, RouteById, RouteIds, RoutePaths } from './routeInfo' import type { AnyRouter, RegisteredRouter, Router } from './router' -import type { BuildLocationFn, NavigateFn } from './RouterProvider' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' @@ -106,13 +110,6 @@ export type RouteOptions< NoInfer > -export interface FullSearchSchemaOption< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, -> { - search: Expand> -} - export type RouteContextFn< in out TParentRoute extends AnyRoute, in out TSearchValidator, @@ -288,30 +285,6 @@ export interface RouteContextOptions< context: Expand> } -export interface RemountDepsOptions< - in out TRouteId, - in out TFullSearchSchema, - in out TAllParams, - in out TLoaderDeps, -> { - routeId: TRouteId - search: TFullSearchSchema - params: TAllParams - loaderDeps: TLoaderDeps -} - -export type MakeRemountDepsOptionsUnion< - TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], - TRoute extends AnyRoute = ParseRoute, -> = TRoute extends any - ? RemountDepsOptions< - TRoute['id'], - TRoute['types']['fullSearchSchema'], - TRoute['types']['allParams'], - TRoute['types']['loaderDeps'] - > - : never - export interface BeforeLoadContextOptions< in out TParentRoute extends AnyRoute, in out TSearchValidator, @@ -571,50 +544,6 @@ export interface LoaderFnContext< route: Route } -export type ResolveFullSearchSchema< - TParentRoute extends AnyRoute, - TSearchValidator, -> = unknown extends TParentRoute - ? ResolveValidatorOutput - : IntersectAssign< - InferFullSearchSchema, - ResolveValidatorOutput - > - -export type ResolveFullSearchSchemaInput< - TParentRoute extends AnyRoute, - TSearchValidator, -> = IntersectAssign< - InferFullSearchSchemaInput, - ResolveSearchValidatorInput -> - -export type RouteContextParameter< - TParentRoute extends AnyRoute, - TRouterContext, -> = unknown extends TParentRoute - ? TRouterContext - : Assign> - -export type BeforeLoadContextParameter< - TParentRoute extends AnyRoute, - TRouterContext, - TRouteContextFn, -> = Assign< - RouteContextParameter, - ContextReturnType -> - -export type ResolveAllContext< - TParentRoute extends AnyRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, -> = Assign< - BeforeLoadContextParameter, - ContextAsyncReturnType -> - export interface AnyRoute extends Route< any, @@ -629,6 +558,7 @@ export interface AnyRoute any, any, any, + any, any > {} @@ -636,11 +566,6 @@ export type AnyRouteWithContext = AnyRoute & { types: { allContext: TContext } } -export type ResolveAllParamsFromParent< - TParentRoute extends AnyRoute, - TParams, -> = Assign, TParams> - export type RouteConstraints = { TParentRoute: AnyRoute TPath: string @@ -749,7 +674,25 @@ export class Route< in out TLoaderDeps extends Record = {}, in out TLoaderFn = undefined, in out TChildren = unknown, -> { + in out TFileRouteTypes = unknown, +> implements + CoreRoute< + TParentRoute, + TPath, + TFullPath, + TCustomId, + TId, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > +{ isRoot: TParentRoute extends Route ? true : false options: RouteOptions< TParentRoute, @@ -847,37 +790,22 @@ export class Route< ) } - types!: { - parentRoute: TParentRoute - path: TPath - to: TrimPathRight - fullPath: TFullPath - customId: TCustomId - id: TId - searchSchema: ResolveValidatorOutput - searchSchemaInput: ResolveSearchValidatorInput - searchValidator: TSearchValidator - fullSearchSchema: ResolveFullSearchSchema - fullSearchSchemaInput: ResolveFullSearchSchemaInput< - TParentRoute, - TSearchValidator - > - params: TParams - allParams: ResolveAllParamsFromParent - routerContext: TRouterContext - routeContext: ResolveRouteContext - routeContextFn: TRouteContextFn - beforeLoadFn: TBeforeLoadFn - allContext: ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - > - children: TChildren - loaderData: ResolveLoaderData - loaderDeps: TLoaderDeps - } + types!: RouteTypes< + TParentRoute, + TPath, + TFullPath, + TCustomId, + TId, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > init = (opts: { originalIndex: number; defaultSsr?: boolean }): void => { this.originalIndex = opts.originalIndex @@ -967,7 +895,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > { return this._addFileChildren(children) as Route< TParentRoute, @@ -982,7 +911,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > } @@ -1001,7 +931,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > { if (Array.isArray(children)) { this.children = children as TChildren @@ -1024,7 +955,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TLoaderFn, - TNewChildren + TNewChildren, + TFileRouteTypes > } @@ -1056,7 +988,8 @@ export class Route< TBeforeLoadFn, TLoaderDeps, TNewLoaderFn, - TChildren + TChildren, + TFileRouteTypes > } @@ -1245,29 +1178,43 @@ export function createRootRouteWithContext() { export const rootRouteWithContext = createRootRouteWithContext export class RootRoute< - in out TSearchValidator = undefined, - in out TRouterContext = {}, - in out TRouteContextFn = AnyContext, - in out TBeforeLoadFn = AnyContext, - in out TLoaderDeps extends Record = {}, - in out TLoaderFn = undefined, - in out TChildren = unknown, - in out TFileRouteTypes = unknown, -> extends Route< - any, // TParentRoute - '/', // TPath - '/', // TFullPath - string, // TCustomId - RootRouteId, // TId - TSearchValidator, // TSearchValidator - {}, // TParams - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, - TLoaderDeps, - TLoaderFn, - TChildren // TChildren -> { + in out TSearchValidator = undefined, + in out TRouterContext = {}, + in out TRouteContextFn = AnyContext, + in out TBeforeLoadFn = AnyContext, + in out TLoaderDeps extends Record = {}, + in out TLoaderFn = undefined, + in out TChildren = unknown, + in out TFileRouteTypes = unknown, + > + extends Route< + any, // TParentRoute + '/', // TPath + '/', // TFullPath + string, // TCustomId + RootRouteId, // TId + TSearchValidator, // TSearchValidator + {}, // TParams + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, // TChildren + TFileRouteTypes + > + implements + CoreRootRoute< + TSearchValidator, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps, + TLoaderFn, + TChildren, + TFileRouteTypes + > +{ /** * @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead. */ diff --git a/packages/solid-router/src/routeInfo.ts b/packages/solid-router/src/routeInfo.ts deleted file mode 100644 index 45bb5a30d33..00000000000 --- a/packages/solid-router/src/routeInfo.ts +++ /dev/null @@ -1,239 +0,0 @@ -import type { InferFileRouteTypes } from './fileRoute' -import type { - AddTrailingSlash, - PartialMergeAll, - RemoveTrailingSlashes, - TrailingSlashOption, -} from '@tanstack/router-core' -import type { AnyRoute } from './route' -import type { AnyRouter } from './router' - -export type ParseRoute = TRouteTree extends { - types: { children: infer TChildren } -} - ? unknown extends TChildren - ? TAcc - : TChildren extends ReadonlyArray - ? ParseRoute - : ParseRoute< - TChildren[keyof TChildren], - TAcc | TChildren[keyof TChildren] - > - : TAcc - -export type ParseRouteWithoutBranches = - ParseRoute extends infer TRoute extends AnyRoute - ? TRoute extends any - ? unknown extends TRoute['types']['children'] - ? TRoute - : TRoute['types']['children'] extends ReadonlyArray - ? '/' extends TRoute['types']['children'][number]['path'] - ? never - : TRoute - : '/' extends TRoute['types']['children'][keyof TRoute['types']['children']]['path'] - ? never - : TRoute - : never - : never - -export type CodeRoutesById = - ParseRoute extends infer TRoutes extends AnyRoute - ? { - [K in TRoutes as K['id']]: K - } - : never - -export type RoutesById = - InferFileRouteTypes extends never - ? CodeRoutesById - : InferFileRouteTypes['fileRoutesById'] - -export type RouteById = Extract< - RoutesById[TId & keyof RoutesById], - AnyRoute -> - -export type CodeRouteIds = - ParseRoute extends infer TRoutes extends AnyRoute - ? TRoutes['id'] - : never - -export type RouteIds = - InferFileRouteTypes extends never - ? CodeRouteIds - : InferFileRouteTypes['id'] - -export type ParentPath = - TrailingSlashOptionByRouter extends 'always' - ? '../' - : TrailingSlashOptionByRouter extends 'never' - ? '..' - : '../' | '..' - -export type CurrentPath = - TrailingSlashOptionByRouter extends 'always' - ? './' - : TrailingSlashOptionByRouter extends 'never' - ? '.' - : './' | '.' - -export type ToPath = - TrailingSlashOptionByRouter extends 'always' - ? AddTrailingSlash - : TrailingSlashOptionByRouter extends 'never' - ? RemoveTrailingSlashes - : AddTrailingSlash | RemoveTrailingSlashes - -export type CatchAllPaths = - | CurrentPath - | ParentPath - -export type CodeRoutesByPath = - ParseRoute extends infer TRoutes extends AnyRoute - ? { - [K in TRoutes as K['fullPath']]: K - } - : never - -export type RoutesByPath = - InferFileRouteTypes extends never - ? CodeRoutesByPath - : InferFileRouteTypes['fileRoutesByFullPath'] - -export type RouteByPath = Extract< - RoutesByPath[TPath & keyof RoutesByPath], - AnyRoute -> - -export type CodeRoutePaths = - ParseRoute extends infer TRoutes extends AnyRoute - ? TRoutes['fullPath'] - : never - -export type RoutePaths = unknown extends TRouteTree - ? string - : - | (InferFileRouteTypes extends never - ? CodeRoutePaths - : InferFileRouteTypes['fullPaths']) - | '/' - -export type RouteToPathAlwaysTrailingSlash = - TRoute['path'] extends '/' - ? TRoute['fullPath'] - : TRoute['fullPath'] extends '/' - ? TRoute['fullPath'] - : `${TRoute['fullPath']}/` - -export type RouteToPathNeverTrailingSlash = - TRoute['path'] extends '/' - ? TRoute['fullPath'] extends '/' - ? TRoute['fullPath'] - : RemoveTrailingSlashes - : TRoute['fullPath'] - -export type RouteToPathPreserveTrailingSlash = - | RouteToPathNeverTrailingSlash - | RouteToPathAlwaysTrailingSlash - -export type RouteToPathByTrailingSlashOption = { - always: RouteToPathAlwaysTrailingSlash - preserve: RouteToPathPreserveTrailingSlash - never: RouteToPathNeverTrailingSlash -} - -export type TrailingSlashOptionByRouter = - TrailingSlashOption extends TRouter['options']['trailingSlash'] - ? 'never' - : NonNullable - -export type RouteToByRouter< - TRouter extends AnyRouter, - TRoute extends AnyRoute, -> = RouteToPathByTrailingSlashOption[TrailingSlashOptionByRouter] - -export type CodeRouteToPath = - ParseRouteWithoutBranches extends infer TRoute extends - AnyRoute - ? TRoute extends any - ? RouteToByRouter - : never - : never - -export type FileRouteToPath< - TRouter extends AnyRouter, - TTo = InferFileRouteTypes['to'], - TTrailingSlashOption = TrailingSlashOptionByRouter, -> = 'never' extends TTrailingSlashOption - ? TTo - : 'always' extends TTrailingSlashOption - ? AddTrailingSlash - : TTo | AddTrailingSlash - -export type RouteToPath = unknown extends TRouter - ? string - : InferFileRouteTypes extends never - ? CodeRouteToPath - : FileRouteToPath - -export type CodeRoutesByToPath = - ParseRouteWithoutBranches extends infer TRoutes extends - AnyRoute - ? { - [TRoute in TRoutes as RouteToByRouter]: TRoute - } - : never - -export type RoutesByToPath = - InferFileRouteTypes extends never - ? CodeRoutesByToPath - : InferFileRouteTypes['fileRoutesByTo'] - -export type CodeRouteByToPath = Extract< - RoutesByToPath[TTo & keyof RoutesByToPath], - AnyRoute -> - -export type FileRouteByToPath = - 'never' extends TrailingSlashOptionByRouter - ? CodeRouteByToPath - : 'always' extends TrailingSlashOptionByRouter - ? TTo extends '/' - ? CodeRouteByToPath - : TTo extends `${infer TPath}/` - ? CodeRouteByToPath - : never - : CodeRouteByToPath< - TRouter, - TTo extends '/' ? TTo : RemoveTrailingSlashes - > - -export type RouteByToPath = - InferFileRouteTypes extends never - ? CodeRouteByToPath - : FileRouteByToPath - -export type FullSearchSchema = - ParseRoute extends infer TRoutes extends AnyRoute - ? PartialMergeAll - : never - -export type FullSearchSchemaInput = - ParseRoute extends infer TRoutes extends AnyRoute - ? PartialMergeAll - : never - -export type AllParams = - ParseRoute extends infer TRoutes extends AnyRoute - ? PartialMergeAll - : never - -export type AllContext = - ParseRoute extends infer TRoutes extends AnyRoute - ? PartialMergeAll - : never - -export type AllLoaderData = - ParseRoute extends infer TRoutes extends AnyRoute - ? PartialMergeAll - : never diff --git a/packages/solid-router/src/router.ts b/packages/solid-router/src/router.ts index 9f7815e9e71..482ee7a1b41 100644 --- a/packages/solid-router/src/router.ts +++ b/packages/solid-router/src/router.ts @@ -42,42 +42,43 @@ import type { AnyContext, AnySchema, AnyValidator, + BuildLocationFn, CommitLocationOptions, ControlledPromise, + FullSearchSchema, + MakeRemountDepsOptionsUnion, Manifest, + NavigateFn, + NavigateOptions, NonNullableUpdater, ParsedLocation, PickAsRequired, + Register, ResolveRelativePath, + RouteById, + RoutePaths, + RoutesById, + RoutesByPath, SearchMiddleware, SearchParser, SearchSerializer, StartSerializer, + ToOptions, TrailingSlashOption, Updater, ViewTransitionOptions, } from '@tanstack/router-core' import type { AnyRoute, - AnyRouteWithContext, BeforeLoadContextOptions, ErrorRouteComponent, LoaderFnContext, - MakeRemountDepsOptionsUnion, NotFoundRouteComponent, RootRoute, RouteComponent, RouteContextOptions, RouteMask, } from './route' - -import type { - FullSearchSchema, - RouteById, - RoutePaths, - RoutesById, - RoutesByPath, -} from './routeInfo' import type { AnyRouteMatch, MakeRouteMatch, @@ -85,11 +86,8 @@ import type { MatchRouteOptions, } from './Matches' -import type { BuildLocationFn, NavigateFn } from './RouterProvider' - import type { AnyRedirect, ResolvedRedirect } from './redirects' import type { NotFoundError } from './not-found' -import type { NavigateOptions, ToOptions } from './link' declare global { interface Window { @@ -97,20 +95,8 @@ declare global { } } -export interface Register { - // router: Router -} - export type AnyRouter = Router -export type AnyRouterWithContext = Router< - AnyRouteWithContext, - any, - any, - any, - any -> - export type RegisteredRouter = Register extends { router: infer TRouter extends AnyRouter } diff --git a/packages/solid-router/src/typePrimitives.ts b/packages/solid-router/src/typePrimitives.ts index 660303ac1fb..cc448533ed8 100644 --- a/packages/solid-router/src/typePrimitives.ts +++ b/packages/solid-router/src/typePrimitives.ts @@ -1,17 +1,18 @@ +import type { LinkComponentProps } from './link' +import type { Redirect } from './redirects' +import type { AnyRouter, RegisteredRouter } from './router' +import type { UseParamsOptions, UseParamsResult } from './useParams' +import type { UseSearchOptions, UseSearchResult } from './useSearch' import type { + Constrain, + ConstrainLiteral, FromPathOption, - LinkComponentProps, NavigateOptions, PathParamOptions, + RouteIds, SearchParamOptions, ToPathOption, -} from './link' -import type { Redirect } from './redirects' -import type { RouteIds } from './routeInfo' -import type { AnyRouter, RegisteredRouter } from './router' -import type { UseParamsOptions, UseParamsResult } from './useParams' -import type { UseSearchOptions, UseSearchResult } from './useSearch' -import type { Constrain, ConstrainLiteral } from '@tanstack/router-core' +} from '@tanstack/router-core' export type ValidateFromPath< TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/solid-router/src/useBlocker.tsx b/packages/solid-router/src/useBlocker.tsx index 487d605d64d..1659f271c55 100644 --- a/packages/solid-router/src/useBlocker.tsx +++ b/packages/solid-router/src/useBlocker.tsx @@ -6,7 +6,7 @@ import type { HistoryLocation, } from '@tanstack/history' import type { AnyRoute, SolidNode } from './route' -import type { ParseRoute } from './routeInfo' +import type { ParseRoute } from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' interface ShouldBlockFnLocation< diff --git a/packages/solid-router/src/useLoaderData.tsx b/packages/solid-router/src/useLoaderData.tsx index 9f25f7ff5e0..25bc698d15e 100644 --- a/packages/solid-router/src/useLoaderData.tsx +++ b/packages/solid-router/src/useLoaderData.tsx @@ -1,9 +1,8 @@ import { useMatch } from './useMatch' import type { Accessor } from 'solid-js' import type { AnyRouter, RegisteredRouter } from './router' -import type { AllLoaderData, RouteById } from './routeInfo' import type { StrictOrFrom } from './utils' -import type { Expand } from '@tanstack/router-core' +import type { AllLoaderData, Expand, RouteById } from '@tanstack/router-core' export interface UseLoaderDataBaseOptions< TRouter extends AnyRouter, diff --git a/packages/solid-router/src/useLoaderDeps.tsx b/packages/solid-router/src/useLoaderDeps.tsx index a62e34866ce..7b8d42cb8bc 100644 --- a/packages/solid-router/src/useLoaderDeps.tsx +++ b/packages/solid-router/src/useLoaderDeps.tsx @@ -1,8 +1,7 @@ import { useMatch } from './useMatch' import type { AnyRouter, RegisteredRouter } from './router' -import type { RouteById } from './routeInfo' import type { StrictOrFrom } from './utils' -import type { Expand } from '@tanstack/router-core' +import type { Expand, RouteById } from '@tanstack/router-core' export interface UseLoaderDepsBaseOptions< TRouter extends AnyRouter, diff --git a/packages/solid-router/src/useNavigate.tsx b/packages/solid-router/src/useNavigate.tsx index 7b25504d147..cba9ea257f5 100644 --- a/packages/solid-router/src/useNavigate.tsx +++ b/packages/solid-router/src/useNavigate.tsx @@ -1,6 +1,6 @@ import * as Solid from 'solid-js' import { useRouter } from './useRouter' -import type { FromPathOption, NavigateOptions } from './link' +import type { FromPathOption, NavigateOptions } from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' export type UseNavigateResult = < diff --git a/packages/solid-router/src/useParams.tsx b/packages/solid-router/src/useParams.tsx index 2a0dcba1942..4d04a9509d8 100644 --- a/packages/solid-router/src/useParams.tsx +++ b/packages/solid-router/src/useParams.tsx @@ -1,10 +1,14 @@ import { useMatch } from './useMatch' import type { ThrowConstraint } from './useMatch' import type { Accessor } from 'solid-js' -import type { AllParams, RouteById } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand, ThrowOrOptional } from '@tanstack/router-core' +import type { + AllParams, + Expand, + RouteById, + ThrowOrOptional, +} from '@tanstack/router-core' export interface UseParamsBaseOptions< TRouter extends AnyRouter, diff --git a/packages/solid-router/src/useRouteContext.ts b/packages/solid-router/src/useRouteContext.ts index b20b47d60b8..3f9b2a877e2 100644 --- a/packages/solid-router/src/useRouteContext.ts +++ b/packages/solid-router/src/useRouteContext.ts @@ -1,9 +1,9 @@ import { useMatch } from './useMatch' import type { Accessor } from 'solid-js' -import type { AllContext, RouteById } from './routeInfo' + import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand } from '@tanstack/router-core' +import type { AllContext, Expand, RouteById } from '@tanstack/router-core' export interface UseRouteContextBaseOptions< TRouter extends AnyRouter, diff --git a/packages/solid-router/src/useSearch.tsx b/packages/solid-router/src/useSearch.tsx index 2cf7fe2e2b5..b1c849e9691 100644 --- a/packages/solid-router/src/useSearch.tsx +++ b/packages/solid-router/src/useSearch.tsx @@ -1,10 +1,14 @@ import { useMatch } from './useMatch' import type { ThrowConstraint } from './useMatch' import type { Accessor } from 'solid-js' -import type { FullSearchSchema, RouteById } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand, ThrowOrOptional } from '@tanstack/router-core' +import type { + Expand, + FullSearchSchema, + RouteById, + ThrowOrOptional, +} from '@tanstack/router-core' export interface UseSearchBaseOptions< TRouter extends AnyRouter, diff --git a/packages/solid-router/src/utils.ts b/packages/solid-router/src/utils.ts index 905c427a47d..aac5ae51c22 100644 --- a/packages/solid-router/src/utils.ts +++ b/packages/solid-router/src/utils.ts @@ -1,7 +1,6 @@ import * as Solid from 'solid-js' -import type { RouteIds } from './routeInfo' import type { AnyRouter } from './router' -import type { ConstrainLiteral } from '@tanstack/router-core' +import type { ConstrainLiteral, RouteIds } from '@tanstack/router-core' export type StrictOrFrom< TRouter extends AnyRouter, diff --git a/packages/solid-router/tests/fileRoute.test-d.tsx b/packages/solid-router/tests/fileRoute.test-d.tsx index f2f7fccc42a..c97d9323252 100644 --- a/packages/solid-router/tests/fileRoute.test-d.tsx +++ b/packages/solid-router/tests/fileRoute.test-d.tsx @@ -17,7 +17,7 @@ const postRoute = createFileRoute('/_postLayout/posts/$postId_')() const protectedRoute = createFileRoute('/(auth)/protected')() -declare module '../src/fileRoute' { +declare module '@tanstack/router-core' { interface FileRoutesByPath { '/': { preLoaderRoute: typeof indexRoute diff --git a/packages/solid-router/tests/lazy/normal.tsx b/packages/solid-router/tests/lazy/normal.tsx index 1addf96c74c..5b0f169da4c 100644 --- a/packages/solid-router/tests/lazy/normal.tsx +++ b/packages/solid-router/tests/lazy/normal.tsx @@ -7,7 +7,7 @@ export function Route(id: string) { } export function FileRoute(id: string) { - return createLazyFileRoute(id as any)({ + return createLazyFileRoute(id as never)({ component: () =>

I'm a normal file route

, }) }