From 342bc3b6fbadb07220da22a0bf2a2875b56eb81c Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 00:40:33 +0100 Subject: [PATCH 1/8] refactor(router-core): move more types to router-core --- packages/react-router/src/fileRoute.ts | 2 +- packages/react-router/src/index.tsx | 6 ++- packages/react-router/src/redirects.ts | 40 ++--------------- packages/react-router/src/route.ts | 5 +-- packages/react-router/src/router.ts | 3 +- packages/react-router/src/typePrimitives.ts | 9 ++-- packages/react-router/src/useLoaderData.tsx | 26 +++-------- packages/react-router/src/useLoaderDeps.tsx | 19 +++----- packages/react-router/src/useMatch.tsx | 3 +- packages/react-router/src/useNavigate.tsx | 17 +++---- packages/react-router/src/useParams.tsx | 26 ++--------- packages/react-router/src/useRouteContext.ts | 47 ++------------------ packages/react-router/src/useSearch.tsx | 26 ++--------- packages/react-router/src/utils.ts | 19 +------- packages/router-core/src/index.ts | 20 +++++++++ packages/router-core/src/redirect.ts | 36 +++++++++++++++ packages/router-core/src/useLoaderData.ts | 20 +++++++++ packages/router-core/src/useLoaderDeps.ts | 13 ++++++ packages/router-core/src/useNavigate.ts | 13 ++++++ packages/router-core/src/useParams.ts | 20 +++++++++ packages/router-core/src/useRouteContext.ts | 46 +++++++++++++++++++ packages/router-core/src/useSearch.ts | 20 +++++++++ packages/router-core/src/utils.ts | 17 +++++++ packages/solid-router/src/fileRoute.ts | 2 +- packages/solid-router/src/index.tsx | 6 ++- packages/solid-router/src/redirects.ts | 40 ++--------------- packages/solid-router/src/route.ts | 5 +-- packages/solid-router/src/router.ts | 3 +- packages/solid-router/src/typePrimitives.ts | 8 ++-- packages/solid-router/src/useLoaderData.tsx | 24 +++------- packages/solid-router/src/useLoaderDeps.tsx | 16 +++---- packages/solid-router/src/useNavigate.tsx | 17 +++---- packages/solid-router/src/useParams.tsx | 23 ++-------- packages/solid-router/src/useRouteContext.ts | 47 ++------------------ packages/solid-router/src/useSearch.tsx | 24 ++-------- 35 files changed, 298 insertions(+), 370 deletions(-) create mode 100644 packages/router-core/src/redirect.ts create mode 100644 packages/router-core/src/useLoaderData.ts create mode 100644 packages/router-core/src/useLoaderDeps.ts create mode 100644 packages/router-core/src/useNavigate.ts create mode 100644 packages/router-core/src/useParams.ts create mode 100644 packages/router-core/src/useRouteContext.ts create mode 100644 packages/router-core/src/useSearch.ts diff --git a/packages/react-router/src/fileRoute.ts b/packages/react-router/src/fileRoute.ts index 82770727671..7fff10dab49 100644 --- a/packages/react-router/src/fileRoute.ts +++ b/packages/react-router/src/fileRoute.ts @@ -29,10 +29,10 @@ import type { ResolveParams, RouteById, RouteIds, + UseRouteContextRoute, } from '@tanstack/router-core' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' -import type { UseRouteContextRoute } from './useRouteContext' export function createFileRoute< TFilePath extends keyof FileRoutesByPath, diff --git a/packages/react-router/src/index.tsx b/packages/react-router/src/index.tsx index aadd9085b38..19483866829 100644 --- a/packages/react-router/src/index.tsx +++ b/packages/react-router/src/index.tsx @@ -195,6 +195,10 @@ export type { AnyRouterWithContext, CommitLocationOptions, MatchLocation, + UseNavigateResult, + AnyRedirect, + Redirect, + ResolvedRedirect, } from '@tanstack/router-core' export type { UseLinkPropsOptions, @@ -232,7 +236,6 @@ export { useLoaderDeps } from './useLoaderDeps' export { useLoaderData } from './useLoaderData' export { redirect, isRedirect } from './redirects' -export type { AnyRedirect, Redirect, ResolvedRedirect } from './redirects' export { RouteApi, @@ -310,7 +313,6 @@ export type { UseBlockerOpts, ShouldBlockFn } from './useBlocker' export { useBlocker, Block } from './useBlocker' export { useNavigate, Navigate } from './useNavigate' -export type { UseNavigateResult } from './useNavigate' export { useParams } from './useParams' export { useSearch } from './useSearch' diff --git a/packages/react-router/src/redirects.ts b/packages/react-router/src/redirects.ts index 348d7aa40d6..3a92d071839 100644 --- a/packages/react-router/src/redirects.ts +++ b/packages/react-router/src/redirects.ts @@ -1,41 +1,9 @@ -import type { AnyRouter, RegisteredRouter } from './router' import type { - NavigateOptions, - PickAsRequired, - RoutePaths, + AnyRedirect, + Redirect, + ResolvedRedirect, } from '@tanstack/router-core' - -export type AnyRedirect = Redirect - -export type Redirect< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends RoutePaths | string = '/', - TTo extends string | undefined = '.', - TMaskFrom extends RoutePaths | string = TFrom, - TMaskTo extends string = '.', -> = { - href?: string - /** - * @deprecated Use `statusCode` instead - **/ - code?: number - statusCode?: number - throw?: any - headers?: HeadersInit -} & NavigateOptions - -export type ResolvedRedirect< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends RoutePaths = '/', - TTo extends string = '', - TMaskFrom extends RoutePaths = TFrom, - TMaskTo extends string = '', -> = PickAsRequired< - Redirect, - 'code' | 'statusCode' | 'headers' -> & { - href: string -} +import type { RegisteredRouter } from './router' export function redirect< TRouter extends RegisteredRouter, diff --git a/packages/react-router/src/route.ts b/packages/react-router/src/route.ts index eafee6a61a1..102d0dcd30e 100644 --- a/packages/react-router/src/route.ts +++ b/packages/react-router/src/route.ts @@ -51,6 +51,8 @@ import type { ToMaskOptions, TrimPathRight, UpdatableStaticRouteOption, + UseNavigateResult, + UseRouteContextRoute, } from '@tanstack/router-core' import type { UseLoaderDataRoute } from './useLoaderData' import type { UseMatchRoute } from './useMatch' @@ -58,7 +60,6 @@ import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseParamsRoute } from './useParams' import type { UseSearchRoute } from './useSearch' import type * as React from 'react' -import type { UseNavigateResult } from './useNavigate' import type { AnyRouteMatch, MakeRouteMatchFromRoute, @@ -69,8 +70,6 @@ import type { AnyRouter, RegisteredRouter, Router } from './router' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' -import type { UseRouteContextRoute } from './useRouteContext' - export type RouteOptions< TParentRoute extends AnyRoute = AnyRoute, TId extends string = string, diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts index 5056de5100b..e86e729c91e 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -41,6 +41,7 @@ import type { NoInfer } from '@tanstack/react-store' import type { AnyContext, + AnyRedirect, AnySchema, AnyValidator, BuildLocationFn, @@ -56,6 +57,7 @@ import type { PickAsRequired, Register, ResolveRelativePath, + ResolvedRedirect, RouteById, RoutePaths, RoutesById, @@ -88,7 +90,6 @@ import type { MatchRouteOptions, } from './Matches' -import type { AnyRedirect, ResolvedRedirect } from './redirects' import type { NotFoundError } from './not-found' declare global { diff --git a/packages/react-router/src/typePrimitives.ts b/packages/react-router/src/typePrimitives.ts index f6b100a0d3a..dde3531820e 100644 --- a/packages/react-router/src/typePrimitives.ts +++ b/packages/react-router/src/typePrimitives.ts @@ -4,15 +4,18 @@ import type { FromPathOption, NavigateOptions, PathParamOptions, + Redirect, RouteIds, SearchParamOptions, ToPathOption, + UseParamsResult, + UseSearchResult, } from '@tanstack/router-core' 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 { UseParamsOptions } from './useParams' +import type { UseSearchOptions } from './useSearch' export type ValidateFromPath< TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/react-router/src/useLoaderData.tsx b/packages/react-router/src/useLoaderData.tsx index d979492bf63..c7dcccc7438 100644 --- a/packages/react-router/src/useLoaderData.tsx +++ b/packages/react-router/src/useLoaderData.tsx @@ -4,8 +4,11 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { AllLoaderData, Expand, RouteById } from '@tanstack/router-core' -import type { StrictOrFrom } from './utils' +import type { + ResolveUseLoaderData, + StrictOrFrom, + UseLoaderDataResult, +} from '@tanstack/router-core' export interface UseLoaderDataBaseOptions< TRouter extends AnyRouter, @@ -15,7 +18,7 @@ export interface UseLoaderDataBaseOptions< TStructuralSharing, > { select?: ( - match: ResolveLoaderData, + match: ResolveUseLoaderData, ) => ValidateSelected } @@ -35,23 +38,6 @@ export type UseLoaderDataOptions< > & StructuralSharingOption -export type ResolveLoaderData< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? AllLoaderData - : Expand['types']['loaderData']> - -export type UseLoaderDataResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveLoaderData - : TSelected - export type UseLoaderDataRoute = < TRouter extends AnyRouter = RegisteredRouter, TSelected = unknown, diff --git a/packages/react-router/src/useLoaderDeps.tsx b/packages/react-router/src/useLoaderDeps.tsx index 326bec856fe..d7d9b51a0f3 100644 --- a/packages/react-router/src/useLoaderDeps.tsx +++ b/packages/react-router/src/useLoaderDeps.tsx @@ -4,8 +4,11 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { StrictOrFrom } from './utils' -import type { Expand, RouteById } from '@tanstack/router-core' +import type { + ResolveUseLoaderDeps, + StrictOrFrom, + UseLoaderDepsResult, +} from '@tanstack/router-core' export interface UseLoaderDepsBaseOptions< TRouter extends AnyRouter, @@ -14,7 +17,7 @@ export interface UseLoaderDepsBaseOptions< TStructuralSharing, > { select?: ( - deps: ResolveLoaderDeps, + deps: ResolveUseLoaderDeps, ) => ValidateSelected } @@ -27,16 +30,6 @@ export type UseLoaderDepsOptions< UseLoaderDepsBaseOptions & StructuralSharingOption -export type ResolveLoaderDeps = Expand< - RouteById['types']['loaderDeps'] -> - -export type UseLoaderDepsResult< - TRouter extends AnyRouter, - TFrom, - TSelected, -> = unknown extends TSelected ? ResolveLoaderDeps : TSelected - export type UseLoaderDepsRoute = < TRouter extends AnyRouter = RegisteredRouter, TSelected = unknown, diff --git a/packages/react-router/src/useMatch.tsx b/packages/react-router/src/useMatch.tsx index 351d29a2fb8..4a7a0aee825 100644 --- a/packages/react-router/src/useMatch.tsx +++ b/packages/react-router/src/useMatch.tsx @@ -8,8 +8,7 @@ import type { } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' import type { MakeRouteMatch, MakeRouteMatchUnion } from './Matches' -import type { StrictOrFrom } from './utils' -import type { ThrowOrOptional } from '@tanstack/router-core' +import type { StrictOrFrom, ThrowOrOptional } from '@tanstack/router-core' export interface UseMatchBaseOptions< TRouter extends AnyRouter, diff --git a/packages/react-router/src/useNavigate.tsx b/packages/react-router/src/useNavigate.tsx index 4d77a52c309..4b023a16c41 100644 --- a/packages/react-router/src/useNavigate.tsx +++ b/packages/react-router/src/useNavigate.tsx @@ -1,19 +1,12 @@ import * as React from 'react' import { useRouter } from './useRouter' -import type { FromPathOption, NavigateOptions } from '@tanstack/router-core' +import type { + FromPathOption, + NavigateOptions, + UseNavigateResult, +} from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' -export type UseNavigateResult = < - TRouter extends RegisteredRouter, - TTo extends string | undefined, - TFrom extends string = TDefaultFrom, - TMaskFrom extends string = TFrom, - TMaskTo extends string = '', ->({ - from, - ...rest -}: NavigateOptions) => Promise - export function useNavigate< TRouter extends AnyRouter = RegisteredRouter, TDefaultFrom extends string = string, diff --git a/packages/react-router/src/useParams.tsx b/packages/react-router/src/useParams.tsx index 82bbe015550..96362c1a2f3 100644 --- a/packages/react-router/src/useParams.tsx +++ b/packages/react-router/src/useParams.tsx @@ -5,12 +5,11 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { StrictOrFrom } from './utils' import type { - AllParams, - Expand, - RouteById, + ResolveUseParams, + StrictOrFrom, ThrowOrOptional, + UseParamsResult, } from '@tanstack/router-core' export interface UseParamsBaseOptions< @@ -22,7 +21,7 @@ export interface UseParamsBaseOptions< TStructuralSharing, > { select?: ( - params: ResolveParams, + params: ResolveUseParams, ) => ValidateSelected shouldThrow?: TThrow } @@ -45,23 +44,6 @@ export type UseParamsOptions< > & StructuralSharingOption -export type ResolveParams< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? AllParams - : Expand['types']['allParams']> - -export type UseParamsResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveParams - : TSelected - export type UseParamsRoute = < TRouter extends AnyRouter = RegisteredRouter, TSelected = unknown, diff --git a/packages/react-router/src/useRouteContext.ts b/packages/react-router/src/useRouteContext.ts index bfc0f32bf97..8096099bf9a 100644 --- a/packages/react-router/src/useRouteContext.ts +++ b/packages/react-router/src/useRouteContext.ts @@ -1,48 +1,9 @@ import { useMatch } from './useMatch' import type { AnyRouter, RegisteredRouter } from './router' -import type { StrictOrFrom } from './utils' -import type { AllContext, Expand, RouteById } from '@tanstack/router-core' - -export interface UseRouteContextBaseOptions< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> { - select?: (search: ResolveRouteContext) => TSelected -} - -export type UseRouteContextOptions< - TRouter extends AnyRouter, - TFrom extends string | undefined, - TStrict extends boolean, - TSelected, -> = StrictOrFrom & - UseRouteContextBaseOptions - -export type ResolveRouteContext< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? AllContext - : Expand['types']['allContext']> - -export type UseRouteContextResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveRouteContext - : TSelected - -export type UseRouteContextRoute = < - TRouter extends AnyRouter = RegisteredRouter, - TSelected = unknown, ->( - opts?: UseRouteContextBaseOptions, -) => UseRouteContextResult +import type { + UseRouteContextOptions, + UseRouteContextResult, +} from '@tanstack/router-core' export function useRouteContext< TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/react-router/src/useSearch.tsx b/packages/react-router/src/useSearch.tsx index 4b36565243b..3b6c86b7add 100644 --- a/packages/react-router/src/useSearch.tsx +++ b/packages/react-router/src/useSearch.tsx @@ -5,12 +5,11 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { StrictOrFrom } from './utils' import type { - Expand, - FullSearchSchema, - RouteById, + ResolveUseSearch, + StrictOrFrom, ThrowOrOptional, + UseSearchResult, } from '@tanstack/router-core' export interface UseSearchBaseOptions< @@ -22,7 +21,7 @@ export interface UseSearchBaseOptions< TStructuralSharing, > { select?: ( - state: ResolveSearch, + state: ResolveUseSearch, ) => ValidateSelected shouldThrow?: TThrow } @@ -45,23 +44,6 @@ export type UseSearchOptions< > & StructuralSharingOption -export type UseSearchResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveSearch - : TSelected - -export type ResolveSearch< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? FullSearchSchema - : Expand['types']['fullSearchSchema']> - export type UseSearchRoute = < TRouter extends AnyRouter = RegisteredRouter, TSelected = unknown, diff --git a/packages/react-router/src/utils.ts b/packages/react-router/src/utils.ts index 048f5c0f68c..2b505fce904 100644 --- a/packages/react-router/src/utils.ts +++ b/packages/react-router/src/utils.ts @@ -1,9 +1,4 @@ import * as React from 'react' -import type { - AnyRouter, - ConstrainLiteral, - RouteIds, -} from '@tanstack/router-core' export function useStableCallback) => any>( fn: T, @@ -15,19 +10,7 @@ export function useStableCallback) => any>( return ref.current as T } -export type StrictOrFrom< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean = true, -> = TStrict extends false - ? { - from?: never - strict: TStrict - } - : { - from: ConstrainLiteral> - strict?: TStrict - } + export const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect diff --git a/packages/router-core/src/index.ts b/packages/router-core/src/index.ts index 4f7a7797f29..6cbcfd7ef2a 100644 --- a/packages/router-core/src/index.ts +++ b/packages/router-core/src/index.ts @@ -245,6 +245,7 @@ export type { MergeAllObjects, MergeAll, ValidateJSON, + StrictOrFrom, } from './utils' export type { @@ -274,3 +275,22 @@ export type { ResolveValidatorOutputFn, ResolveValidatorOutput, } from './validators' + +export type { + UseRouteContextRoute, + UseRouteContextBaseOptions, + UseRouteContextOptions, + UseRouteContextResult, +} from './useRouteContext' + +export type { UseSearchResult, ResolveUseSearch } from './useSearch' + +export type { UseParamsResult, ResolveUseParams } from './useParams' + +export type { UseNavigateResult } from './useNavigate' + +export type { UseLoaderDepsResult, ResolveUseLoaderDeps } from './useLoaderDeps' + +export type { UseLoaderDataResult, ResolveUseLoaderData } from './useLoaderData' + +export type { Redirect, ResolvedRedirect, AnyRedirect } from './redirect' diff --git a/packages/router-core/src/redirect.ts b/packages/router-core/src/redirect.ts new file mode 100644 index 00000000000..8642f43b42b --- /dev/null +++ b/packages/router-core/src/redirect.ts @@ -0,0 +1,36 @@ +import type { NavigateOptions } from './link' +import type { RoutePaths } from './routeInfo' +import type { AnyRouter, RegisteredRouter } from './router' +import type { PickAsRequired } from './utils' + +export type AnyRedirect = Redirect + +export type Redirect< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends RoutePaths | string = '/', + TTo extends string | undefined = '.', + TMaskFrom extends RoutePaths | string = TFrom, + TMaskTo extends string = '.', +> = { + href?: string + /** + * @deprecated Use `statusCode` instead + **/ + code?: number + statusCode?: number + throw?: any + headers?: HeadersInit +} & NavigateOptions + +export type ResolvedRedirect< + TRouter extends AnyRouter = RegisteredRouter, + TFrom extends RoutePaths = '/', + TTo extends string = '', + TMaskFrom extends RoutePaths = TFrom, + TMaskTo extends string = '', +> = PickAsRequired< + Redirect, + 'code' | 'statusCode' | 'headers' +> & { + href: string +} diff --git a/packages/router-core/src/useLoaderData.ts b/packages/router-core/src/useLoaderData.ts new file mode 100644 index 00000000000..f3167fc7e41 --- /dev/null +++ b/packages/router-core/src/useLoaderData.ts @@ -0,0 +1,20 @@ +import type { AllLoaderData, RouteById } from './routeInfo' +import type { AnyRouter } from './router' +import type { Expand } from './utils' + +export type ResolveUseLoaderData< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, +> = TStrict extends false + ? AllLoaderData + : Expand['types']['loaderData']> + +export type UseLoaderDataResult< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, + TSelected, +> = unknown extends TSelected + ? ResolveUseLoaderData + : TSelected diff --git a/packages/router-core/src/useLoaderDeps.ts b/packages/router-core/src/useLoaderDeps.ts new file mode 100644 index 00000000000..4a1634702cc --- /dev/null +++ b/packages/router-core/src/useLoaderDeps.ts @@ -0,0 +1,13 @@ +import type { RouteById } from './routeInfo' +import type { AnyRouter } from './router' +import type { Expand } from './utils' + +export type ResolveUseLoaderDeps = Expand< + RouteById['types']['loaderDeps'] +> + +export type UseLoaderDepsResult< + TRouter extends AnyRouter, + TFrom, + TSelected, +> = unknown extends TSelected ? ResolveUseLoaderDeps : TSelected diff --git a/packages/router-core/src/useNavigate.ts b/packages/router-core/src/useNavigate.ts new file mode 100644 index 00000000000..ca0282bccec --- /dev/null +++ b/packages/router-core/src/useNavigate.ts @@ -0,0 +1,13 @@ +import type { NavigateOptions } from './link' +import type { RegisteredRouter } from './router' + +export type UseNavigateResult = < + TRouter extends RegisteredRouter, + TTo extends string | undefined, + TFrom extends string = TDefaultFrom, + TMaskFrom extends string = TFrom, + TMaskTo extends string = '', +>({ + from, + ...rest +}: NavigateOptions) => Promise diff --git a/packages/router-core/src/useParams.ts b/packages/router-core/src/useParams.ts new file mode 100644 index 00000000000..4938ed7f057 --- /dev/null +++ b/packages/router-core/src/useParams.ts @@ -0,0 +1,20 @@ +import type { AllParams, RouteById } from './routeInfo' +import type { AnyRouter } from './router' +import type { Expand } from './utils' + +export type ResolveUseParams< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, +> = TStrict extends false + ? AllParams + : Expand['types']['allParams']> + +export type UseParamsResult< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, + TSelected, +> = unknown extends TSelected + ? ResolveUseParams + : TSelected diff --git a/packages/router-core/src/useRouteContext.ts b/packages/router-core/src/useRouteContext.ts new file mode 100644 index 00000000000..c1f2395e0da --- /dev/null +++ b/packages/router-core/src/useRouteContext.ts @@ -0,0 +1,46 @@ +import type { AllContext, RouteById } from './routeInfo' +import type { AnyRouter, RegisteredRouter } from './router' +import type { Expand, StrictOrFrom } from './utils' + +export interface UseRouteContextBaseOptions< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, + TSelected, +> { + select?: ( + search: ResolveUseRouteContext, + ) => TSelected +} + +export type UseRouteContextOptions< + TRouter extends AnyRouter, + TFrom extends string | undefined, + TStrict extends boolean, + TSelected, +> = StrictOrFrom & + UseRouteContextBaseOptions + +export type ResolveUseRouteContext< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, +> = TStrict extends false + ? AllContext + : Expand['types']['allContext']> + +export type UseRouteContextResult< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, + TSelected, +> = unknown extends TSelected + ? ResolveUseRouteContext + : TSelected + +export type UseRouteContextRoute = < + TRouter extends AnyRouter = RegisteredRouter, + TSelected = unknown, +>( + opts?: UseRouteContextBaseOptions, +) => UseRouteContextResult diff --git a/packages/router-core/src/useSearch.ts b/packages/router-core/src/useSearch.ts new file mode 100644 index 00000000000..2abe0ecaa3f --- /dev/null +++ b/packages/router-core/src/useSearch.ts @@ -0,0 +1,20 @@ +import type { FullSearchSchema, RouteById } from './routeInfo' +import type { AnyRouter } from './router' +import type { Expand } from './utils' + +export type UseSearchResult< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, + TSelected, +> = unknown extends TSelected + ? ResolveUseSearch + : TSelected + +export type ResolveUseSearch< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean, +> = TStrict extends false + ? FullSearchSchema + : Expand['types']['fullSearchSchema']> diff --git a/packages/router-core/src/utils.ts b/packages/router-core/src/utils.ts index 87c40aac34c..1ffc5e505b8 100644 --- a/packages/router-core/src/utils.ts +++ b/packages/router-core/src/utils.ts @@ -1,3 +1,6 @@ +import type { RouteIds } from './routeInfo' +import type { AnyRouter } from './router' + export type NoInfer = [T][T extends any ? 0 : never] export type IsAny = 1 extends 0 & TValue ? TYesResult @@ -330,6 +333,20 @@ export type ThrowOrOptional = TThrow extends true ? T : T | undefined +export type StrictOrFrom< + TRouter extends AnyRouter, + TFrom, + TStrict extends boolean = true, +> = TStrict extends false + ? { + from?: never + strict: TStrict + } + : { + from: ConstrainLiteral> + strict?: TStrict + } + export type ControlledPromise = Promise & { resolve: (value: T) => void reject: (value: any) => void diff --git a/packages/solid-router/src/fileRoute.ts b/packages/solid-router/src/fileRoute.ts index 34a3031b4a3..bb710c7f156 100644 --- a/packages/solid-router/src/fileRoute.ts +++ b/packages/solid-router/src/fileRoute.ts @@ -19,6 +19,7 @@ import type { ResolveParams, RouteById, RouteIds, + UseRouteContextRoute, } from '@tanstack/router-core' import type { @@ -32,7 +33,6 @@ import type { import type { RegisteredRouter } from './router' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' -import type { UseRouteContextRoute } from './useRouteContext' export function createFileRoute< TFilePath extends keyof FileRoutesByPath, diff --git a/packages/solid-router/src/index.tsx b/packages/solid-router/src/index.tsx index c40bae7d81a..e3ff1a97c77 100644 --- a/packages/solid-router/src/index.tsx +++ b/packages/solid-router/src/index.tsx @@ -165,6 +165,10 @@ export type { RemountDepsOptions, FileRouteTypes, FileRoutesByPath, + UseNavigateResult, + AnyRedirect, + Redirect, + ResolvedRedirect, } from '@tanstack/router-core' export { @@ -238,7 +242,6 @@ export { useLoaderDeps } from './useLoaderDeps' export { useLoaderData } from './useLoaderData' export { redirect, isRedirect } from './redirects' -export type { AnyRedirect, Redirect, ResolvedRedirect } from './redirects' export { RouteApi, @@ -318,7 +321,6 @@ export type { UseBlockerOpts, ShouldBlockFn } from './useBlocker' export { useBlocker, Block } from './useBlocker' export { useNavigate, Navigate } from './useNavigate' -export type { UseNavigateResult } from './useNavigate' export { useParams } from './useParams' export { useSearch } from './useSearch' diff --git a/packages/solid-router/src/redirects.ts b/packages/solid-router/src/redirects.ts index 348d7aa40d6..aca4388d09e 100644 --- a/packages/solid-router/src/redirects.ts +++ b/packages/solid-router/src/redirects.ts @@ -1,42 +1,10 @@ -import type { AnyRouter, RegisteredRouter } from './router' +import type { RegisteredRouter } from './router' import type { - NavigateOptions, - PickAsRequired, - RoutePaths, + AnyRedirect, + Redirect, + ResolvedRedirect, } from '@tanstack/router-core' -export type AnyRedirect = Redirect - -export type Redirect< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends RoutePaths | string = '/', - TTo extends string | undefined = '.', - TMaskFrom extends RoutePaths | string = TFrom, - TMaskTo extends string = '.', -> = { - href?: string - /** - * @deprecated Use `statusCode` instead - **/ - code?: number - statusCode?: number - throw?: any - headers?: HeadersInit -} & NavigateOptions - -export type ResolvedRedirect< - TRouter extends AnyRouter = RegisteredRouter, - TFrom extends RoutePaths = '/', - TTo extends string = '', - TMaskFrom extends RoutePaths = TFrom, - TMaskTo extends string = '', -> = PickAsRequired< - Redirect, - 'code' | 'statusCode' | 'headers' -> & { - href: string -} - export function redirect< TRouter extends RegisteredRouter, const TTo extends string | undefined, diff --git a/packages/solid-router/src/route.ts b/packages/solid-router/src/route.ts index 131021c0579..128995de5d9 100644 --- a/packages/solid-router/src/route.ts +++ b/packages/solid-router/src/route.ts @@ -51,6 +51,8 @@ import type { ToMaskOptions, TrimPathRight, UpdatableStaticRouteOption, + UseNavigateResult, + UseRouteContextRoute, } from '@tanstack/router-core' import type { UseLoaderDataRoute } from './useLoaderData' import type { UseMatchRoute } from './useMatch' @@ -58,7 +60,6 @@ import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseParamsRoute } from './useParams' import type { UseSearchRoute } from './useSearch' import type * as Solid from 'solid-js' -import type { UseNavigateResult } from './useNavigate' import type { AnyRouteMatch, MakeRouteMatchFromRoute, @@ -69,8 +70,6 @@ import type { AnyRouter, RegisteredRouter, Router } from './router' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' -import type { UseRouteContextRoute } from './useRouteContext' - export type RouteOptions< TParentRoute extends AnyRoute = AnyRoute, TId extends string = string, diff --git a/packages/solid-router/src/router.ts b/packages/solid-router/src/router.ts index 482ee7a1b41..07b7d957991 100644 --- a/packages/solid-router/src/router.ts +++ b/packages/solid-router/src/router.ts @@ -40,6 +40,7 @@ import type { NoInfer } from '@tanstack/solid-store' import type { AnyContext, + AnyRedirect, AnySchema, AnyValidator, BuildLocationFn, @@ -54,6 +55,7 @@ import type { ParsedLocation, PickAsRequired, Register, + ResolvedRedirect, ResolveRelativePath, RouteById, RoutePaths, @@ -86,7 +88,6 @@ import type { MatchRouteOptions, } from './Matches' -import type { AnyRedirect, ResolvedRedirect } from './redirects' import type { NotFoundError } from './not-found' declare global { diff --git a/packages/solid-router/src/typePrimitives.ts b/packages/solid-router/src/typePrimitives.ts index cc448533ed8..d934fd7cb70 100644 --- a/packages/solid-router/src/typePrimitives.ts +++ b/packages/solid-router/src/typePrimitives.ts @@ -1,17 +1,19 @@ 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 { UseParamsOptions } from './useParams' +import type { UseSearchOptions } from './useSearch' import type { Constrain, ConstrainLiteral, FromPathOption, NavigateOptions, PathParamOptions, + Redirect, RouteIds, SearchParamOptions, ToPathOption, + UseParamsResult, + UseSearchResult, } from '@tanstack/router-core' export type ValidateFromPath< diff --git a/packages/solid-router/src/useLoaderData.tsx b/packages/solid-router/src/useLoaderData.tsx index 25bc698d15e..636fd8defc4 100644 --- a/packages/solid-router/src/useLoaderData.tsx +++ b/packages/solid-router/src/useLoaderData.tsx @@ -2,7 +2,10 @@ import { useMatch } from './useMatch' import type { Accessor } from 'solid-js' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { AllLoaderData, Expand, RouteById } from '@tanstack/router-core' +import type { + ResolveUseLoaderData, + UseLoaderDataResult, +} from '@tanstack/router-core' export interface UseLoaderDataBaseOptions< TRouter extends AnyRouter, @@ -10,7 +13,7 @@ export interface UseLoaderDataBaseOptions< TStrict extends boolean, TSelected, > { - select?: (match: ResolveLoaderData) => TSelected + select?: (match: ResolveUseLoaderData) => TSelected } export type UseLoaderDataOptions< @@ -21,23 +24,6 @@ export type UseLoaderDataOptions< > = StrictOrFrom & UseLoaderDataBaseOptions -export type ResolveLoaderData< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? AllLoaderData - : Expand['types']['loaderData']> - -export type UseLoaderDataResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveLoaderData - : TSelected - export type UseLoaderDataRoute = < TRouter extends AnyRouter = RegisteredRouter, TSelected = unknown, diff --git a/packages/solid-router/src/useLoaderDeps.tsx b/packages/solid-router/src/useLoaderDeps.tsx index 7b8d42cb8bc..e91d0fe6c56 100644 --- a/packages/solid-router/src/useLoaderDeps.tsx +++ b/packages/solid-router/src/useLoaderDeps.tsx @@ -1,14 +1,17 @@ import { useMatch } from './useMatch' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' -import type { Expand, RouteById } from '@tanstack/router-core' +import type { + ResolveUseLoaderDeps, + UseLoaderDepsResult, +} from '@tanstack/router-core' export interface UseLoaderDepsBaseOptions< TRouter extends AnyRouter, TFrom, TSelected, > { - select?: (deps: ResolveLoaderDeps) => TSelected + select?: (deps: ResolveUseLoaderDeps) => TSelected } export type UseLoaderDepsOptions< @@ -17,15 +20,6 @@ export type UseLoaderDepsOptions< TSelected, > = StrictOrFrom & UseLoaderDepsBaseOptions -export type ResolveLoaderDeps = Expand< - RouteById['types']['loaderDeps'] -> - -export type UseLoaderDepsResult< - TRouter extends AnyRouter, - TFrom, - TSelected, -> = unknown extends TSelected ? ResolveLoaderDeps : TSelected export type UseLoaderDepsRoute = < TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/solid-router/src/useNavigate.tsx b/packages/solid-router/src/useNavigate.tsx index cba9ea257f5..2d135ab2432 100644 --- a/packages/solid-router/src/useNavigate.tsx +++ b/packages/solid-router/src/useNavigate.tsx @@ -1,19 +1,12 @@ import * as Solid from 'solid-js' import { useRouter } from './useRouter' -import type { FromPathOption, NavigateOptions } from '@tanstack/router-core' +import type { + FromPathOption, + NavigateOptions, + UseNavigateResult, +} from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter } from './router' -export type UseNavigateResult = < - TRouter extends RegisteredRouter, - TTo extends string | undefined, - TFrom extends string = TDefaultFrom, - TMaskFrom extends string = TFrom, - TMaskTo extends string = '', ->({ - from, - ...rest -}: NavigateOptions) => Promise - export function useNavigate< TRouter extends AnyRouter = RegisteredRouter, TDefaultFrom extends string = string, diff --git a/packages/solid-router/src/useParams.tsx b/packages/solid-router/src/useParams.tsx index 4d04a9509d8..57a5e821be2 100644 --- a/packages/solid-router/src/useParams.tsx +++ b/packages/solid-router/src/useParams.tsx @@ -4,10 +4,9 @@ import type { Accessor } from 'solid-js' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' import type { - AllParams, - Expand, - RouteById, + ResolveUseParams, ThrowOrOptional, + UseParamsResult, } from '@tanstack/router-core' export interface UseParamsBaseOptions< @@ -17,7 +16,7 @@ export interface UseParamsBaseOptions< TThrow extends boolean, TSelected, > { - select?: (params: ResolveParams) => TSelected + select?: (params: ResolveUseParams) => TSelected shouldThrow?: TThrow } @@ -29,22 +28,6 @@ export type UseParamsOptions< TSelected, > = StrictOrFrom & UseParamsBaseOptions -export type ResolveParams< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? AllParams - : Expand['types']['allParams']> - -export type UseParamsResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveParams - : TSelected export type UseParamsRoute = < TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/solid-router/src/useRouteContext.ts b/packages/solid-router/src/useRouteContext.ts index 3f9b2a877e2..6a3324dd5ed 100644 --- a/packages/solid-router/src/useRouteContext.ts +++ b/packages/solid-router/src/useRouteContext.ts @@ -2,49 +2,10 @@ import { useMatch } from './useMatch' import type { Accessor } from 'solid-js' import type { AnyRouter, RegisteredRouter } from './router' -import type { StrictOrFrom } from './utils' -import type { AllContext, Expand, RouteById } from '@tanstack/router-core' - -export interface UseRouteContextBaseOptions< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> { - select?: (search: ResolveRouteContext) => TSelected -} - -export type UseRouteContextOptions< - TRouter extends AnyRouter, - TFrom extends string | undefined, - TStrict extends boolean, - TSelected, -> = StrictOrFrom & - UseRouteContextBaseOptions - -export type ResolveRouteContext< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? AllContext - : Expand['types']['allContext']> - -export type UseRouteContextResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveRouteContext - : TSelected - -export type UseRouteContextRoute = < - TRouter extends AnyRouter = RegisteredRouter, - TSelected = unknown, ->( - opts?: UseRouteContextBaseOptions, -) => Accessor> +import type { + UseRouteContextOptions, + UseRouteContextResult, +} from '@tanstack/router-core' export function useRouteContext< TRouter extends AnyRouter = RegisteredRouter, diff --git a/packages/solid-router/src/useSearch.tsx b/packages/solid-router/src/useSearch.tsx index b1c849e9691..bb6586ce96a 100644 --- a/packages/solid-router/src/useSearch.tsx +++ b/packages/solid-router/src/useSearch.tsx @@ -4,10 +4,9 @@ import type { Accessor } from 'solid-js' import type { AnyRouter, RegisteredRouter } from './router' import type { StrictOrFrom } from './utils' import type { - Expand, - FullSearchSchema, - RouteById, + ResolveUseSearch, ThrowOrOptional, + UseSearchResult, } from '@tanstack/router-core' export interface UseSearchBaseOptions< @@ -17,7 +16,7 @@ export interface UseSearchBaseOptions< TThrow extends boolean, TSelected, > { - select?: (state: ResolveSearch) => TSelected + select?: (state: ResolveUseSearch) => TSelected shouldThrow?: TThrow } @@ -30,23 +29,6 @@ export type UseSearchOptions< > = StrictOrFrom & UseSearchBaseOptions -export type UseSearchResult< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, - TSelected, -> = unknown extends TSelected - ? ResolveSearch - : TSelected - -export type ResolveSearch< - TRouter extends AnyRouter, - TFrom, - TStrict extends boolean, -> = TStrict extends false - ? FullSearchSchema - : Expand['types']['fullSearchSchema']> - export type UseSearchRoute = < TRouter extends AnyRouter = RegisteredRouter, TSelected = unknown, From 32d7de931a0e56db4ee545b3c2fcef5db73de77b Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 23 Feb 2025 23:42:25 +0000 Subject: [PATCH 2/8] ci: apply automated fixes --- packages/react-router/src/utils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/react-router/src/utils.ts b/packages/react-router/src/utils.ts index 2b505fce904..d78025e0861 100644 --- a/packages/react-router/src/utils.ts +++ b/packages/react-router/src/utils.ts @@ -10,8 +10,6 @@ export function useStableCallback) => any>( return ref.current as T } - - export const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect From 355d4cb67ba990826fcb2e492801764a4cc01028 Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 00:47:16 +0100 Subject: [PATCH 3/8] chore: fix eslint --- packages/solid-router/src/router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/solid-router/src/router.ts b/packages/solid-router/src/router.ts index 07b7d957991..c379c6ebcf3 100644 --- a/packages/solid-router/src/router.ts +++ b/packages/solid-router/src/router.ts @@ -55,8 +55,8 @@ import type { ParsedLocation, PickAsRequired, Register, - ResolvedRedirect, ResolveRelativePath, + ResolvedRedirect, RouteById, RoutePaths, RoutesById, From 9c02b9fcadc011db2d5b47c42976343423ee4344 Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 01:02:04 +0100 Subject: [PATCH 4/8] chore: add accessor --- packages/solid-router/src/route.ts | 2 +- packages/solid-router/src/useRouteContext.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/solid-router/src/route.ts b/packages/solid-router/src/route.ts index 128995de5d9..7b620645b6b 100644 --- a/packages/solid-router/src/route.ts +++ b/packages/solid-router/src/route.ts @@ -52,7 +52,6 @@ import type { TrimPathRight, UpdatableStaticRouteOption, UseNavigateResult, - UseRouteContextRoute, } from '@tanstack/router-core' import type { UseLoaderDataRoute } from './useLoaderData' import type { UseMatchRoute } from './useMatch' @@ -69,6 +68,7 @@ import type { import type { AnyRouter, RegisteredRouter, Router } from './router' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' +import type { UseRouteContextRoute } from './useRouteContext' export type RouteOptions< TParentRoute extends AnyRoute = AnyRoute, diff --git a/packages/solid-router/src/useRouteContext.ts b/packages/solid-router/src/useRouteContext.ts index 6a3324dd5ed..0ae39977d67 100644 --- a/packages/solid-router/src/useRouteContext.ts +++ b/packages/solid-router/src/useRouteContext.ts @@ -3,10 +3,18 @@ import type { Accessor } from 'solid-js' import type { AnyRouter, RegisteredRouter } from './router' import type { + UseRouteContextBaseOptions, UseRouteContextOptions, UseRouteContextResult, } from '@tanstack/router-core' +export type UseRouteContextRoute = < + TRouter extends AnyRouter = RegisteredRouter, + TSelected = unknown, +>( + opts?: UseRouteContextBaseOptions, +) => Accessor> + export function useRouteContext< TRouter extends AnyRouter = RegisteredRouter, const TFrom extends string | undefined = undefined, From 369c56c9fa84988a2c3af2dd749cdfd605923602 Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 01:11:05 +0100 Subject: [PATCH 5/8] refactor(router-core): revert accessor specific --- packages/react-router/src/fileRoute.ts | 2 +- packages/react-router/src/route.ts | 2 +- packages/react-router/src/useRouteContext.ts | 8 ++++++++ packages/router-core/src/index.ts | 1 - packages/router-core/src/useRouteContext.ts | 9 +-------- packages/solid-router/src/fileRoute.ts | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/react-router/src/fileRoute.ts b/packages/react-router/src/fileRoute.ts index 7fff10dab49..82770727671 100644 --- a/packages/react-router/src/fileRoute.ts +++ b/packages/react-router/src/fileRoute.ts @@ -29,10 +29,10 @@ import type { ResolveParams, RouteById, RouteIds, - UseRouteContextRoute, } from '@tanstack/router-core' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' +import type { UseRouteContextRoute } from './useRouteContext' export function createFileRoute< TFilePath extends keyof FileRoutesByPath, diff --git a/packages/react-router/src/route.ts b/packages/react-router/src/route.ts index 102d0dcd30e..04f0209a360 100644 --- a/packages/react-router/src/route.ts +++ b/packages/react-router/src/route.ts @@ -52,7 +52,6 @@ import type { TrimPathRight, UpdatableStaticRouteOption, UseNavigateResult, - UseRouteContextRoute, } from '@tanstack/router-core' import type { UseLoaderDataRoute } from './useLoaderData' import type { UseMatchRoute } from './useMatch' @@ -69,6 +68,7 @@ import type { import type { AnyRouter, RegisteredRouter, Router } from './router' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' +import type { UseRouteContextRoute } from './useRouteContext' export type RouteOptions< TParentRoute extends AnyRoute = AnyRoute, diff --git a/packages/react-router/src/useRouteContext.ts b/packages/react-router/src/useRouteContext.ts index 8096099bf9a..a77a5935d9b 100644 --- a/packages/react-router/src/useRouteContext.ts +++ b/packages/react-router/src/useRouteContext.ts @@ -1,10 +1,18 @@ import { useMatch } from './useMatch' import type { AnyRouter, RegisteredRouter } from './router' import type { + UseRouteContextBaseOptions, UseRouteContextOptions, UseRouteContextResult, } from '@tanstack/router-core' +export type UseRouteContextRoute = < + TRouter extends AnyRouter = RegisteredRouter, + TSelected = unknown, +>( + opts?: UseRouteContextBaseOptions, +) => UseRouteContextResult + export function useRouteContext< TRouter extends AnyRouter = RegisteredRouter, const TFrom extends string | undefined = undefined, diff --git a/packages/router-core/src/index.ts b/packages/router-core/src/index.ts index 6cbcfd7ef2a..aec2a70f1ea 100644 --- a/packages/router-core/src/index.ts +++ b/packages/router-core/src/index.ts @@ -277,7 +277,6 @@ export type { } from './validators' export type { - UseRouteContextRoute, UseRouteContextBaseOptions, UseRouteContextOptions, UseRouteContextResult, diff --git a/packages/router-core/src/useRouteContext.ts b/packages/router-core/src/useRouteContext.ts index c1f2395e0da..8cfcd197ce1 100644 --- a/packages/router-core/src/useRouteContext.ts +++ b/packages/router-core/src/useRouteContext.ts @@ -1,5 +1,5 @@ import type { AllContext, RouteById } from './routeInfo' -import type { AnyRouter, RegisteredRouter } from './router' +import type { AnyRouter } from './router' import type { Expand, StrictOrFrom } from './utils' export interface UseRouteContextBaseOptions< @@ -37,10 +37,3 @@ export type UseRouteContextResult< > = unknown extends TSelected ? ResolveUseRouteContext : TSelected - -export type UseRouteContextRoute = < - TRouter extends AnyRouter = RegisteredRouter, - TSelected = unknown, ->( - opts?: UseRouteContextBaseOptions, -) => UseRouteContextResult diff --git a/packages/solid-router/src/fileRoute.ts b/packages/solid-router/src/fileRoute.ts index bb710c7f156..34a3031b4a3 100644 --- a/packages/solid-router/src/fileRoute.ts +++ b/packages/solid-router/src/fileRoute.ts @@ -19,7 +19,6 @@ import type { ResolveParams, RouteById, RouteIds, - UseRouteContextRoute, } from '@tanstack/router-core' import type { @@ -33,6 +32,7 @@ import type { import type { RegisteredRouter } from './router' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' +import type { UseRouteContextRoute } from './useRouteContext' export function createFileRoute< TFilePath extends keyof FileRoutesByPath, From 34179edfd34f7e071786d8f3b2faa56193508687 Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 19:01:33 +0100 Subject: [PATCH 6/8] fix: use AnyCoreRoute to fix declaration error --- packages/react-router/src/fileRoute.ts | 17 ++++++++++++----- packages/solid-router/src/fileRoute.ts | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/react-router/src/fileRoute.ts b/packages/react-router/src/fileRoute.ts index 82770727671..aeb77f2be8b 100644 --- a/packages/react-router/src/fileRoute.ts +++ b/packages/react-router/src/fileRoute.ts @@ -22,9 +22,12 @@ import type { import type { RegisteredRouter } from './router' import type { AnyContext, + AnyRoute as AnyCoreRoute, AnyPathParams, + AnyRouter, AnyValidator, Constrain, + ConstrainLiteral, FileRoutesByPath, ResolveParams, RouteById, @@ -175,7 +178,7 @@ export type LazyRouteOptions = Pick< 'component' | 'errorComponent' | 'pendingComponent' | 'notFoundComponent' > -export class LazyRoute { +export class LazyRoute { options: { id: string } & LazyRouteOptions @@ -236,11 +239,15 @@ export class LazyRoute { } export function createLazyRoute< - TId extends RouteIds, - TRoute extends AnyRoute = RouteById, ->(id: TId) { + TRouter extends AnyRouter = RegisteredRouter, + TId extends string = string, + TRoute extends AnyCoreRoute = RouteById, +>(id: ConstrainLiteral>) { return (opts: LazyRouteOptions) => { - return new LazyRoute({ id: id as any, ...opts }) + return new LazyRoute({ + id: id, + ...opts, + }) } } diff --git a/packages/solid-router/src/fileRoute.ts b/packages/solid-router/src/fileRoute.ts index 34a3031b4a3..dfaa88a77db 100644 --- a/packages/solid-router/src/fileRoute.ts +++ b/packages/solid-router/src/fileRoute.ts @@ -12,9 +12,12 @@ import type { UseMatchRoute } from './useMatch' import type { UseSearchRoute } from './useSearch' import type { AnyContext, + AnyRoute as AnyCoreRoute, AnyPathParams, + AnyRouter, AnyValidator, Constrain, + ConstrainLiteral, FileRoutesByPath, ResolveParams, RouteById, @@ -175,7 +178,7 @@ export type LazyRouteOptions = Pick< 'component' | 'errorComponent' | 'pendingComponent' | 'notFoundComponent' > -export class LazyRoute { +export class LazyRoute { options: { id: string } & LazyRouteOptions @@ -230,14 +233,17 @@ export class LazyRoute { } export function createLazyRoute< - TId extends RouteIds, - TRoute extends AnyRoute = RouteById, ->(id: TId) { + TRouter extends AnyRouter = RegisteredRouter, + TId extends string = string, + TRoute extends AnyCoreRoute = RouteById, +>(id: ConstrainLiteral>) { return (opts: LazyRouteOptions) => { - return new LazyRoute({ id: id as any, ...opts }) + return new LazyRoute({ + id: id, + ...opts, + }) } } - export function createLazyFileRoute< TFilePath extends keyof FileRoutesByPath, TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'], From 944dc4a797dc8eb076e1fb00cdd5a6f191a9bb61 Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 21:36:43 +0100 Subject: [PATCH 7/8] refactor(router-core): move matches and route options --- packages/react-router/src/Matches.tsx | 114 +---- packages/react-router/src/fileRoute.ts | 12 +- packages/react-router/src/index.tsx | 30 +- packages/react-router/src/route.ts | 540 +-------------------- packages/react-router/src/router.ts | 16 +- packages/react-router/src/useMatch.tsx | 8 +- packages/router-core/src/Matches.ts | 115 ++++- packages/router-core/src/index.ts | 19 + packages/router-core/src/route.ts | 527 +++++++++++++++++++- packages/router-core/src/typePrimitives.ts | 0 packages/solid-router/src/Matches.tsx | 113 +---- packages/solid-router/src/fileRoute.ts | 12 +- packages/solid-router/src/index.tsx | 28 +- packages/solid-router/src/route.ts | 540 +-------------------- packages/solid-router/src/router.ts | 16 +- packages/solid-router/src/useMatch.tsx | 7 +- 16 files changed, 755 insertions(+), 1342 deletions(-) create mode 100644 packages/router-core/src/typePrimitives.ts diff --git a/packages/react-router/src/Matches.tsx b/packages/react-router/src/Matches.tsx index 27e9b376413..3e05b192886 100644 --- a/packages/react-router/src/Matches.tsx +++ b/packages/react-router/src/Matches.tsx @@ -11,111 +11,30 @@ import type { StructuralSharingOption, ValidateSelected, } from './structuralSharing' -import type { AnyRoute, ReactNode } from './route' +import type { ReactNode } from './route' import type { - AllContext, - AllLoaderData, - AllParams, - ControlledPromise, DeepPartial, - FullSearchSchema, MakeOptionalPathParams, MakeOptionalSearchParams, + MakeRouteMatchUnion, MaskOptions, NoInfer, - ParseRoute, ResolveRelativePath, ResolveRoute, - RouteById, RouteByPath, - RouteIds, - StaticDataRouteOption, ToSubOptionsProps, } from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter, RouterState } from './router' -export type MakeRouteMatchFromRoute = RouteMatch< - TRoute['types']['id'], - TRoute['types']['fullPath'], - TRoute['types']['allParams'], - TRoute['types']['fullSearchSchema'], - TRoute['types']['loaderData'], - TRoute['types']['allContext'], - TRoute['types']['loaderDeps'] -> - -export interface RouteMatch< - out TRouteId, - out TFullPath, - out TAllParams, - out TFullSearchSchema, - out TLoaderData, - out TAllContext, - out TLoaderDeps, -> { - id: string - routeId: TRouteId - fullPath: TFullPath - index: number - pathname: string - params: TAllParams - _strictParams: TAllParams - status: 'pending' | 'success' | 'error' | 'redirected' | 'notFound' - isFetching: false | 'beforeLoad' | 'loader' - error: unknown - paramsError: unknown - searchError: unknown - updatedAt: number - loadPromise?: ControlledPromise - beforeLoadPromise?: ControlledPromise - loaderPromise?: ControlledPromise - loaderData?: TLoaderData - __routeContext: Record - __beforeLoadContext: Record - context: TAllContext - search: TFullSearchSchema - _strictSearch: TFullSearchSchema - fetchCount: number - abortController: AbortController - cause: 'preload' | 'enter' | 'stay' - loaderDeps: TLoaderDeps - preload: boolean - invalid: boolean - meta?: Array - links?: Array - scripts?: Array - headScripts?: Array - headers?: Record - globalNotFound?: boolean - staticData: StaticDataRouteOption - minPendingPromise?: ControlledPromise - pendingTimeout?: ReturnType +declare module '@tanstack/router-core' { + export interface RouteMatchExtensions { + meta?: Array + links?: Array + scripts?: Array + headScripts?: Array + } } -export type MakeRouteMatch< - TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], - TRouteId = RouteIds, - TStrict extends boolean = true, -> = RouteMatch< - TRouteId, - RouteById['types']['fullPath'], - TStrict extends false - ? AllParams - : RouteById['types']['allParams'], - TStrict extends false - ? FullSearchSchema - : RouteById['types']['fullSearchSchema'], - TStrict extends false - ? AllLoaderData - : RouteById['types']['loaderData'], - TStrict extends false - ? AllContext - : RouteById['types']['allContext'], - RouteById['types']['loaderDeps'] -> - -export type AnyRouteMatch = RouteMatch - export function Matches() { const router = useRouter() @@ -257,21 +176,6 @@ export function MatchRoute< return params ? props.children : null } -export type MakeRouteMatchUnion< - TRouter extends AnyRouter = RegisteredRouter, - TRoute extends AnyRoute = ParseRoute, -> = TRoute extends any - ? RouteMatch< - TRoute['id'], - TRoute['fullPath'], - TRoute['types']['allParams'], - TRoute['types']['fullSearchSchema'], - TRoute['types']['loaderData'], - TRoute['types']['allContext'], - TRoute['types']['loaderDeps'] - > - : never - export interface UseMatchesBaseOptions< TRouter extends AnyRouter, TSelected, diff --git a/packages/react-router/src/fileRoute.ts b/packages/react-router/src/fileRoute.ts index aeb77f2be8b..bc8cc774841 100644 --- a/packages/react-router/src/fileRoute.ts +++ b/packages/react-router/src/fileRoute.ts @@ -11,14 +11,7 @@ import type { UseParamsRoute } from './useParams' import type { UseMatchRoute } from './useMatch' import type { UseSearchRoute } from './useSearch' -import type { - AnyRoute, - FileBaseRouteOptions, - Route, - RouteConstraints, - RouteLoaderFn, - UpdatableRouteOptions, -} from './route' +import type { AnyRoute, Route, RouteConstraints } from './route' import type { RegisteredRouter } from './router' import type { AnyContext, @@ -28,10 +21,13 @@ import type { AnyValidator, Constrain, ConstrainLiteral, + FileBaseRouteOptions, FileRoutesByPath, ResolveParams, RouteById, RouteIds, + RouteLoaderFn, + UpdatableRouteOptions, } from '@tanstack/router-core' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' diff --git a/packages/react-router/src/index.tsx b/packages/react-router/src/index.tsx index 19483866829..f435ead4972 100644 --- a/packages/react-router/src/index.tsx +++ b/packages/react-router/src/index.tsx @@ -199,6 +199,21 @@ export type { AnyRedirect, Redirect, ResolvedRedirect, + MakeRouteMatch, + MakeRouteMatchUnion, + RouteMatch, + AnyRouteMatch, + RouteContextFn, + RouteContextOptions, + BeforeLoadFn, + BeforeLoadContextOptions, + ContextOptions, + RouteOptions, + FileBaseRouteOptions, + BaseRouteOptions, + UpdatableRouteOptions, + RouteLoaderFn, + LoaderFnContext, } from '@tanstack/router-core' export type { UseLinkPropsOptions, @@ -219,13 +234,9 @@ export { } from './Matches' export type { - RouteMatch, - AnyRouteMatch, MatchRouteOptions, UseMatchRouteOptions, MakeMatchRouteOptions, - MakeRouteMatch, - MakeRouteMatchUnion, } from './Matches' export { matchContext } from './matchContext' @@ -250,12 +261,6 @@ export { NotFoundRoute, } from './route' export type { - RouteOptions, - FileBaseRouteOptions, - BaseRouteOptions, - UpdatableRouteOptions, - RouteLoaderFn, - LoaderFnContext, AnyRoute, RouteConstraints, AnyRootRoute, @@ -267,11 +272,6 @@ export type { ErrorRouteComponent, NotFoundRouteComponent, RootRouteOptions, - RouteContextFn, - RouteContextOptions, - BeforeLoadFn, - BeforeLoadContextOptions, - ContextOptions, } from './route' export { diff --git a/packages/react-router/src/route.ts b/packages/react-router/src/route.ts index 04f0209a360..495a2a59fe8 100644 --- a/packages/react-router/src/route.ts +++ b/packages/react-router/src/route.ts @@ -9,48 +9,29 @@ import { useNavigate } from './useNavigate' import { useMatch } from './useMatch' import type { AnyContext, - AnyPathParams, AnySchema, - AnyValidator, - BeforeLoadContextParameter, - BuildLocationFn, Constrain, ConstrainLiteral, RootRoute as CoreRootRoute, Route as CoreRoute, - DefaultValidator, ErrorComponentProps, - Expand, - FullSearchSchemaOption, - NavigateFn, - NavigateOptions, - NoInfer, NotFoundRouteProps, - ParamsOptions, - ParsedLocation, - RemountDepsOptions, - ResolveAllContext, - ResolveAllParamsFromParent, ResolveFullPath, - ResolveFullSearchSchema, - ResolveFullSearchSchemaInput, ResolveId, - ResolveLoaderData, ResolveParams, RootRouteId, + RootRouteOptions, RouteById, RouteContext, - RouteContextParameter, RouteIds, - RoutePathOptions, + RouteLoaderFn, + RouteOptions, RoutePathOptionsIntersection, RoutePaths, RouteTypes, - SearchFilter, - SearchMiddleware, ToMaskOptions, TrimPathRight, - UpdatableStaticRouteOption, + UpdatableRouteOptions, UseNavigateResult, } from '@tanstack/router-core' import type { UseLoaderDataRoute } from './useLoaderData' @@ -59,487 +40,19 @@ import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseParamsRoute } from './useParams' import type { UseSearchRoute } from './useSearch' import type * as React from 'react' -import type { - AnyRouteMatch, - MakeRouteMatchFromRoute, - MakeRouteMatchUnion, - RouteMatch, -} from './Matches' +import type {} from './Matches' import type { AnyRouter, RegisteredRouter, Router } from './router' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' import type { UseRouteContextRoute } from './useRouteContext' -export type RouteOptions< - TParentRoute extends AnyRoute = AnyRoute, - TId extends string = string, - TCustomId extends string = string, - TFullPath extends string = string, - TPath extends string = string, - TSearchValidator = undefined, - TParams = AnyPathParams, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, -> = BaseRouteOptions< - TParentRoute, - TId, - TCustomId, - TPath, - TSearchValidator, - TParams, - TLoaderDeps, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn -> & - UpdatableRouteOptions< - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer - > - -export type RouteContextFn< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, - in out TParams, - in out TRouterContext, -> = ( - ctx: RouteContextOptions< - TParentRoute, - TSearchValidator, - TParams, - TRouterContext - >, -) => any - -export type BeforeLoadFn< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, - in out TParams, - in out TRouterContext, - in out TRouteContextFn, -> = ( - ctx: BeforeLoadContextOptions< - TParentRoute, - TSearchValidator, - TParams, - TRouterContext, - TRouteContextFn - >, -) => any - -export type FileBaseRouteOptions< - TParentRoute extends AnyRoute = AnyRoute, - TId extends string = string, - TPath extends string = string, - TSearchValidator = undefined, - TParams = {}, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, - TRemountDepsFn = AnyContext, -> = ParamsOptions & { - validateSearch?: Constrain - - shouldReload?: - | boolean - | (( - match: LoaderFnContext< - TParentRoute, - TId, - TParams, - TLoaderDeps, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - ) => any) - - context?: Constrain< - TRouteContextFn, - ( - ctx: RouteContextOptions< - TParentRoute, - TParams, - TRouterContext, - TLoaderDeps - >, - ) => any - > - - // This async function is called before a route is loaded. - // If an error is thrown here, the route's loader will not be called. - // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function. - // If thrown during a preload event, the error will be logged to the console. - beforeLoad?: Constrain< - TBeforeLoadFn, - ( - ctx: BeforeLoadContextOptions< - TParentRoute, - TSearchValidator, - TParams, - TRouterContext, - TRouteContextFn - >, - ) => any - > - - loaderDeps?: ( - opts: FullSearchSchemaOption, - ) => TLoaderDeps - - remountDeps?: Constrain< - TRemountDepsFn, - ( - opt: RemountDepsOptions< - TId, - FullSearchSchemaOption, - Expand>, - TLoaderDeps - >, - ) => any - > - - loader?: Constrain< - TLoaderFn, - ( - ctx: LoaderFnContext< - TParentRoute, - TId, - TParams, - TLoaderDeps, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - ) => any - > -} - -export type BaseRouteOptions< - TParentRoute extends AnyRoute = AnyRoute, - TId extends string = string, - TCustomId extends string = string, - TPath extends string = string, - TSearchValidator = undefined, - TParams = {}, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, -> = RoutePathOptions & - FileBaseRouteOptions< - TParentRoute, - TId, - TPath, - TSearchValidator, - TParams, - TLoaderDeps, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - > & { - getParentRoute: () => TParentRoute +declare module '@tanstack/router-core' { + export interface UpdatableRouteOptionsExtensions { + component?: RouteComponent + errorComponent?: false | null | ErrorRouteComponent + notFoundComponent?: NotFoundRouteComponent + pendingComponent?: RouteComponent } - -export interface ContextOptions< - in out TParentRoute extends AnyRoute, - in out TParams, -> { - abortController: AbortController - preload: boolean - params: Expand> - location: ParsedLocation - /** - * @deprecated Use `throw redirect({ to: '/somewhere' })` instead - **/ - navigate: NavigateFn - buildLocation: BuildLocationFn - cause: 'preload' | 'enter' | 'stay' - matches: Array -} - -export interface RouteContextOptions< - in out TParentRoute extends AnyRoute, - in out TParams, - in out TRouterContext, - in out TLoaderDeps, -> extends ContextOptions { - deps: TLoaderDeps - context: Expand> -} - -export interface BeforeLoadContextOptions< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, - in out TParams, - in out TRouterContext, - in out TRouteContextFn, -> extends ContextOptions, - FullSearchSchemaOption { - context: Expand< - BeforeLoadContextParameter - > -} - -type AssetFnContextOptions< - in out TRouteId, - in out TFullPath, - in out TParentRoute extends AnyRoute, - in out TParams, - in out TSearchValidator, - in out TLoaderFn, - in out TRouterContext, - in out TRouteContextFn, - in out TBeforeLoadFn, - in out TLoaderDeps, -> = { - matches: Array< - RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - > - > - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - > - params: ResolveAllParamsFromParent - loaderData: ResolveLoaderData -} -export interface UpdatableRouteOptions< - in out TParentRoute extends AnyRoute, - in out TRouteId, - in out TFullPath, - in out TParams, - in out TSearchValidator, - in out TLoaderFn, - in out TLoaderDeps, - in out TRouterContext, - in out TRouteContextFn, - in out TBeforeLoadFn, -> extends UpdatableStaticRouteOption { - // If true, this route will be matched as case-sensitive - caseSensitive?: boolean - // If true, this route will be forcefully wrapped in a suspense boundary - wrapInSuspense?: boolean - // The content to be rendered when the route is matched. If no component is provided, defaults to `` - component?: RouteComponent - errorComponent?: false | null | ErrorRouteComponent - notFoundComponent?: NotFoundRouteComponent - pendingComponent?: RouteComponent - pendingMs?: number - pendingMinMs?: number - staleTime?: number - gcTime?: number - preload?: boolean - preloadStaleTime?: number - preloadGcTime?: number - search?: { - middlewares?: Array< - SearchMiddleware< - ResolveFullSearchSchemaInput - > - > - } - /** - @deprecated Use search.middlewares instead - */ - preSearchFilters?: Array< - SearchFilter> - > - /** - @deprecated Use search.middlewares instead - */ - postSearchFilters?: Array< - SearchFilter> - > - onCatch?: (error: Error, errorInfo: React.ErrorInfo) => void - onError?: (err: any) => void - // These functions are called as route matches are loaded, stick around and leave the active - // matches - onEnter?: ( - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - >, - ) => void - onStay?: ( - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - >, - ) => void - onLeave?: ( - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - >, - ) => void - headers?: (ctx: { - loaderData: ResolveLoaderData - }) => Record - head?: ( - ctx: AssetFnContextOptions< - TRouteId, - TFullPath, - TParentRoute, - TParams, - TSearchValidator, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, - TLoaderDeps - >, - ) => { - links?: AnyRouteMatch['links'] - scripts?: AnyRouteMatch['headScripts'] - meta?: AnyRouteMatch['meta'] - } - scripts?: ( - ctx: AssetFnContextOptions< - TRouteId, - TFullPath, - TParentRoute, - TParams, - TSearchValidator, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, - TLoaderDeps - >, - ) => AnyRouteMatch['scripts'] - ssr?: boolean - codeSplitGroupings?: Array< - Array< - | 'loader' - | 'component' - | 'pendingComponent' - | 'notFoundComponent' - | 'errorComponent' - > - > -} - -export type RouteLoaderFn< - in out TParentRoute extends AnyRoute = AnyRoute, - in out TId extends string = string, - in out TParams = {}, - in out TLoaderDeps = {}, - in out TRouterContext = {}, - in out TRouteContextFn = AnyContext, - in out TBeforeLoadFn = AnyContext, -> = ( - match: LoaderFnContext< - TParentRoute, - TId, - TParams, - TLoaderDeps, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, -) => any - -export interface LoaderFnContext< - in out TParentRoute extends AnyRoute = AnyRoute, - in out TId extends string = string, - in out TParams = {}, - in out TLoaderDeps = {}, - in out TRouterContext = {}, - in out TRouteContextFn = AnyContext, - in out TBeforeLoadFn = AnyContext, -> { - abortController: AbortController - preload: boolean - params: Expand> - deps: TLoaderDeps - context: Expand< - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - > - > - location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps - /** - * @deprecated Use `throw redirect({ to: '/somewhere' })` instead - **/ - navigate: (opts: NavigateOptions) => Promise | void - // root route does not have a parent match - parentMatchPromise: TId extends RootRouteId - ? never - : Promise> - cause: 'preload' | 'enter' | 'stay' - route: Route } export interface AnyRoute @@ -1119,37 +632,6 @@ export function createRoute< export type AnyRootRoute = RootRoute -export type RootRouteOptions< - TSearchValidator = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, -> = Omit< - RouteOptions< - any, // TParentRoute - RootRouteId, // TId - RootRouteId, // TCustomId - '', // TFullPath - '', // TPath - TSearchValidator, - {}, // TParams - TLoaderDeps, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - | 'path' - | 'id' - | 'getParentRoute' - | 'caseSensitive' - | 'parseParams' - | 'stringifyParams' - | 'params' -> - export function createRootRouteWithContext() { return < TRouteContextFn = AnyContext, diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts index e86e729c91e..e4f1a0d4676 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -42,13 +42,18 @@ import type { NoInfer } from '@tanstack/react-store' import type { AnyContext, AnyRedirect, + AnyRouteMatch, AnySchema, AnyValidator, + BeforeLoadContextOptions, BuildLocationFn, CommitLocationOptions, ControlledPromise, FullSearchSchema, + LoaderFnContext, MakeRemountDepsOptionsUnion, + MakeRouteMatch, + MakeRouteMatchUnion, Manifest, NavigateFn, NavigateOptions, @@ -59,6 +64,7 @@ import type { ResolveRelativePath, ResolvedRedirect, RouteById, + RouteContextOptions, RoutePaths, RoutesById, RoutesByPath, @@ -73,22 +79,14 @@ import type { } from '@tanstack/router-core' import type { AnyRoute, - BeforeLoadContextOptions, ErrorRouteComponent, - LoaderFnContext, NotFoundRouteComponent, RootRoute, RouteComponent, - RouteContextOptions, RouteMask, } from './route' -import type { - AnyRouteMatch, - MakeRouteMatch, - MakeRouteMatchUnion, - MatchRouteOptions, -} from './Matches' +import type { MatchRouteOptions } from './Matches' import type { NotFoundError } from './not-found' diff --git a/packages/react-router/src/useMatch.tsx b/packages/react-router/src/useMatch.tsx index 4a7a0aee825..09ddb4999a9 100644 --- a/packages/react-router/src/useMatch.tsx +++ b/packages/react-router/src/useMatch.tsx @@ -7,8 +7,12 @@ import type { ValidateSelected, } from './structuralSharing' import type { AnyRouter, RegisteredRouter } from './router' -import type { MakeRouteMatch, MakeRouteMatchUnion } from './Matches' -import type { StrictOrFrom, ThrowOrOptional } from '@tanstack/router-core' +import type { + MakeRouteMatch, + MakeRouteMatchUnion, + StrictOrFrom, + ThrowOrOptional, +} from '@tanstack/router-core' export interface UseMatchBaseOptions< TRouter extends AnyRouter, diff --git a/packages/router-core/src/Matches.ts b/packages/router-core/src/Matches.ts index 6ff0f7390fc..2eb838b630e 100644 --- a/packages/router-core/src/Matches.ts +++ b/packages/router-core/src/Matches.ts @@ -1,4 +1,15 @@ -import type { Constrain } from './utils' +import type { AnyRoute, StaticDataRouteOption } from './route' +import type { + AllContext, + AllLoaderData, + AllParams, + FullSearchSchema, + ParseRoute, + RouteById, + RouteIds, +} from './routeInfo' +import type { AnyRouter, RegisteredRouter } from './router' +import type { Constrain, ControlledPromise } from './utils' export type AnyMatchAndValue = { match: any; value: any } @@ -92,3 +103,105 @@ export const isMatch = ( return value != null } + +export interface DefaultRouteMatchExtensions { + scripts?: unknown + links?: unknown + headScripts?: unknown + meta?: unknown +} + +export interface RouteMatchExtensions extends DefaultRouteMatchExtensions {} + +export interface RouteMatch< + out TRouteId, + out TFullPath, + out TAllParams, + out TFullSearchSchema, + out TLoaderData, + out TAllContext, + out TLoaderDeps, +> extends RouteMatchExtensions { + id: string + routeId: TRouteId + fullPath: TFullPath + index: number + pathname: string + params: TAllParams + _strictParams: TAllParams + status: 'pending' | 'success' | 'error' | 'redirected' | 'notFound' + isFetching: false | 'beforeLoad' | 'loader' + error: unknown + paramsError: unknown + searchError: unknown + updatedAt: number + loadPromise?: ControlledPromise + beforeLoadPromise?: ControlledPromise + loaderPromise?: ControlledPromise + loaderData?: TLoaderData + __routeContext: Record + __beforeLoadContext: Record + context: TAllContext + search: TFullSearchSchema + _strictSearch: TFullSearchSchema + fetchCount: number + abortController: AbortController + cause: 'preload' | 'enter' | 'stay' + loaderDeps: TLoaderDeps + preload: boolean + invalid: boolean + headers?: Record + globalNotFound?: boolean + staticData: StaticDataRouteOption + minPendingPromise?: ControlledPromise + pendingTimeout?: ReturnType +} + +export type MakeRouteMatchFromRoute = RouteMatch< + TRoute['types']['id'], + TRoute['types']['fullPath'], + TRoute['types']['allParams'], + TRoute['types']['fullSearchSchema'], + TRoute['types']['loaderData'], + TRoute['types']['allContext'], + TRoute['types']['loaderDeps'] +> + +export type MakeRouteMatch< + TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], + TRouteId = RouteIds, + TStrict extends boolean = true, +> = RouteMatch< + TRouteId, + RouteById['types']['fullPath'], + TStrict extends false + ? AllParams + : RouteById['types']['allParams'], + TStrict extends false + ? FullSearchSchema + : RouteById['types']['fullSearchSchema'], + TStrict extends false + ? AllLoaderData + : RouteById['types']['loaderData'], + TStrict extends false + ? AllContext + : RouteById['types']['allContext'], + RouteById['types']['loaderDeps'] +> + +export type AnyRouteMatch = RouteMatch + +export type MakeRouteMatchUnion< + TRouter extends AnyRouter = RegisteredRouter, + TRoute extends AnyRoute = ParseRoute, +> = TRoute extends any + ? RouteMatch< + TRoute['id'], + TRoute['fullPath'], + TRoute['types']['allParams'], + TRoute['types']['fullSearchSchema'], + TRoute['types']['loaderData'], + TRoute['types']['allContext'], + TRoute['types']['loaderDeps'] + > + : never diff --git a/packages/router-core/src/index.ts b/packages/router-core/src/index.ts index aec2a70f1ea..7bf9df7663e 100644 --- a/packages/router-core/src/index.ts +++ b/packages/router-core/src/index.ts @@ -85,6 +85,12 @@ export type { IsMatchResult, IsMatchParse, IsMatch, + RouteMatch, + RouteMatchExtensions, + MakeRouteMatchUnion, + MakeRouteMatch, + AnyRouteMatch, + MakeRouteMatchFromRoute, } from './Matches' export { joinPaths, @@ -167,6 +173,19 @@ export type { MakeRemountDepsOptionsUnion, ResolveFullPath, AnyRouteWithContext, + RouteOptions, + FileBaseRouteOptions, + BaseRouteOptions, + UpdatableRouteOptions, + RouteLoaderFn, + LoaderFnContext, + RouteContextFn, + RouteContextOptions, + BeforeLoadFn, + BeforeLoadContextOptions, + ContextOptions, + RootRouteOptions, + UpdatableRouteOptionsExtensions, } from './route' export { defaultSerializeError, getLocationChangeInfo } from './router' diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index d7345403c0e..ac2d17747de 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1,13 +1,23 @@ -import type { ParsePathParams } from './link' +import type { NavigateOptions, ParsePathParams } from './link' +import type { ParsedLocation } from './location' +import type { + AnyRouteMatch, + MakeRouteMatchFromRoute, + MakeRouteMatchUnion, + RouteMatch, +} from './Matches' import type { RootRouteId } from './root' import type { ParseRoute } from './routeInfo' -import type { RegisteredRouter } from './router' -import type { Assign, Expand, IntersectAssign } from './utils' +import type { AnyRouter, RegisteredRouter } from './router' +import type { BuildLocationFn, NavigateFn } from './RouterProvider' +import type { Assign, Constrain, Expand, IntersectAssign } from './utils' import type { AnySchema, AnyStandardSchemaValidator, + AnyValidator, AnyValidatorAdapter, AnyValidatorObj, + DefaultValidator, ResolveSearchValidatorInput, ResolveValidatorOutput, StandardSchemaValidator, @@ -503,6 +513,517 @@ export type AnyRouteWithContext = AnyRoute & { types: { allContext: TContext } } +export type RouteOptions< + TParentRoute extends AnyRoute = AnyRoute, + TId extends string = string, + TCustomId extends string = string, + TFullPath extends string = string, + TPath extends string = string, + TSearchValidator = undefined, + TParams = AnyPathParams, + TLoaderDeps extends Record = {}, + TLoaderFn = undefined, + TRouterContext = {}, + TRouteContextFn = AnyContext, + TBeforeLoadFn = AnyContext, +> = BaseRouteOptions< + TParentRoute, + TId, + TCustomId, + TPath, + TSearchValidator, + TParams, + TLoaderDeps, + TLoaderFn, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn +> & + UpdatableRouteOptions< + NoInfer, + NoInfer, + NoInfer, + NoInfer, + NoInfer, + NoInfer, + NoInfer, + NoInfer, + NoInfer, + NoInfer + > + +export type RouteContextFn< + in out TParentRoute extends AnyRoute, + in out TSearchValidator, + in out TParams, + in out TRouterContext, +> = ( + ctx: RouteContextOptions< + TParentRoute, + TSearchValidator, + TParams, + TRouterContext + >, +) => any + +export type BeforeLoadFn< + in out TParentRoute extends AnyRoute, + in out TSearchValidator, + in out TParams, + in out TRouterContext, + in out TRouteContextFn, +> = ( + ctx: BeforeLoadContextOptions< + TParentRoute, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn + >, +) => any + +export type FileBaseRouteOptions< + TParentRoute extends AnyRoute = AnyRoute, + TId extends string = string, + TPath extends string = string, + TSearchValidator = undefined, + TParams = {}, + TLoaderDeps extends Record = {}, + TLoaderFn = undefined, + TRouterContext = {}, + TRouteContextFn = AnyContext, + TBeforeLoadFn = AnyContext, + TRemountDepsFn = AnyContext, +> = ParamsOptions & { + validateSearch?: Constrain + + shouldReload?: + | boolean + | (( + match: LoaderFnContext< + TParentRoute, + TId, + TParams, + TLoaderDeps, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + ) => any) + + context?: Constrain< + TRouteContextFn, + ( + ctx: RouteContextOptions< + TParentRoute, + TParams, + TRouterContext, + TLoaderDeps + >, + ) => any + > + + // This async function is called before a route is loaded. + // If an error is thrown here, the route's loader will not be called. + // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function. + // If thrown during a preload event, the error will be logged to the console. + beforeLoad?: Constrain< + TBeforeLoadFn, + ( + ctx: BeforeLoadContextOptions< + TParentRoute, + TSearchValidator, + TParams, + TRouterContext, + TRouteContextFn + >, + ) => any + > + + loaderDeps?: ( + opts: FullSearchSchemaOption, + ) => TLoaderDeps + + remountDeps?: Constrain< + TRemountDepsFn, + ( + opt: RemountDepsOptions< + TId, + FullSearchSchemaOption, + Expand>, + TLoaderDeps + >, + ) => any + > + + loader?: Constrain< + TLoaderFn, + ( + ctx: LoaderFnContext< + TParentRoute, + TId, + TParams, + TLoaderDeps, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + ) => any + > +} + +export type BaseRouteOptions< + TParentRoute extends AnyRoute = AnyRoute, + TId extends string = string, + TCustomId extends string = string, + TPath extends string = string, + TSearchValidator = undefined, + TParams = {}, + TLoaderDeps extends Record = {}, + TLoaderFn = undefined, + TRouterContext = {}, + TRouteContextFn = AnyContext, + TBeforeLoadFn = AnyContext, +> = RoutePathOptions & + FileBaseRouteOptions< + TParentRoute, + TId, + TPath, + TSearchValidator, + TParams, + TLoaderDeps, + TLoaderFn, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + > & { + getParentRoute: () => TParentRoute + } + +export interface ContextOptions< + in out TParentRoute extends AnyRoute, + in out TParams, +> { + abortController: AbortController + preload: boolean + params: Expand> + location: ParsedLocation + /** + * @deprecated Use `throw redirect({ to: '/somewhere' })` instead + **/ + navigate: NavigateFn + buildLocation: BuildLocationFn + cause: 'preload' | 'enter' | 'stay' + matches: Array +} + +export interface RouteContextOptions< + in out TParentRoute extends AnyRoute, + in out TParams, + in out TRouterContext, + in out TLoaderDeps, +> extends ContextOptions { + deps: TLoaderDeps + context: Expand> +} + +export interface BeforeLoadContextOptions< + in out TParentRoute extends AnyRoute, + in out TSearchValidator, + in out TParams, + in out TRouterContext, + in out TRouteContextFn, +> extends ContextOptions, + FullSearchSchemaOption { + context: Expand< + BeforeLoadContextParameter + > +} + +type AssetFnContextOptions< + in out TRouteId, + in out TFullPath, + in out TParentRoute extends AnyRoute, + in out TParams, + in out TSearchValidator, + in out TLoaderFn, + in out TRouterContext, + in out TRouteContextFn, + in out TBeforeLoadFn, + in out TLoaderDeps, +> = { + matches: Array< + RouteMatch< + TRouteId, + TFullPath, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveLoaderData, + ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + TLoaderDeps + > + > + match: RouteMatch< + TRouteId, + TFullPath, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveLoaderData, + ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + TLoaderDeps + > + params: ResolveAllParamsFromParent + loaderData: ResolveLoaderData +} + +export interface DefaultUpdatableRouteOptionsExtensions { + component?: unknown + errorComponent?: unknown + notFoundComponent?: unknown + pendingComponent?: unknown +} + +export interface UpdatableRouteOptionsExtensions {} + +export interface UpdatableRouteOptions< + in out TParentRoute extends AnyRoute, + in out TRouteId, + in out TFullPath, + in out TParams, + in out TSearchValidator, + in out TLoaderFn, + in out TLoaderDeps, + in out TRouterContext, + in out TRouteContextFn, + in out TBeforeLoadFn, +> extends UpdatableStaticRouteOption, + UpdatableRouteOptionsExtensions { + // If true, this route will be matched as case-sensitive + caseSensitive?: boolean + // If true, this route will be forcefully wrapped in a suspense boundary + wrapInSuspense?: boolean + // The content to be rendered when the route is matched. If no component is provided, defaults to `` + + pendingMs?: number + pendingMinMs?: number + staleTime?: number + gcTime?: number + preload?: boolean + preloadStaleTime?: number + preloadGcTime?: number + search?: { + middlewares?: Array< + SearchMiddleware< + ResolveFullSearchSchemaInput + > + > + } + /** + @deprecated Use search.middlewares instead + */ + preSearchFilters?: Array< + SearchFilter> + > + /** + @deprecated Use search.middlewares instead + */ + postSearchFilters?: Array< + SearchFilter> + > + onCatch?: (error: Error) => void + onError?: (err: any) => void + // These functions are called as route matches are loaded, stick around and leave the active + // matches + onEnter?: ( + match: RouteMatch< + TRouteId, + TFullPath, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveLoaderData, + ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + TLoaderDeps + >, + ) => void + onStay?: ( + match: RouteMatch< + TRouteId, + TFullPath, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveLoaderData, + ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + TLoaderDeps + >, + ) => void + onLeave?: ( + match: RouteMatch< + TRouteId, + TFullPath, + ResolveAllParamsFromParent, + ResolveFullSearchSchema, + ResolveLoaderData, + ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + TLoaderDeps + >, + ) => void + headers?: (ctx: { + loaderData: ResolveLoaderData + }) => Record + head?: ( + ctx: AssetFnContextOptions< + TRouteId, + TFullPath, + TParentRoute, + TParams, + TSearchValidator, + TLoaderFn, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps + >, + ) => { + links?: AnyRouteMatch['links'] + scripts?: AnyRouteMatch['headScripts'] + meta?: AnyRouteMatch['meta'] + } + scripts?: ( + ctx: AssetFnContextOptions< + TRouteId, + TFullPath, + TParentRoute, + TParams, + TSearchValidator, + TLoaderFn, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn, + TLoaderDeps + >, + ) => AnyRouteMatch['scripts'] + ssr?: boolean + codeSplitGroupings?: Array< + Array< + | 'loader' + | 'component' + | 'pendingComponent' + | 'notFoundComponent' + | 'errorComponent' + > + > +} + +export type RouteLoaderFn< + in out TParentRoute extends AnyRoute = AnyRoute, + in out TId extends string = string, + in out TParams = {}, + in out TLoaderDeps = {}, + in out TRouterContext = {}, + in out TRouteContextFn = AnyContext, + in out TBeforeLoadFn = AnyContext, +> = ( + match: LoaderFnContext< + TParentRoute, + TId, + TParams, + TLoaderDeps, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, +) => any + +export interface LoaderFnContext< + in out TParentRoute extends AnyRoute = AnyRoute, + in out TId extends string = string, + in out TParams = {}, + in out TLoaderDeps = {}, + in out TRouterContext = {}, + in out TRouteContextFn = AnyContext, + in out TBeforeLoadFn = AnyContext, +> { + abortController: AbortController + preload: boolean + params: Expand> + deps: TLoaderDeps + context: Expand< + ResolveAllContext< + TParentRoute, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + > + > + location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps + /** + * @deprecated Use `throw redirect({ to: '/somewhere' })` instead + **/ + navigate: (opts: NavigateOptions) => Promise | void + // root route does not have a parent match + parentMatchPromise: TId extends RootRouteId + ? never + : Promise> + cause: 'preload' | 'enter' | 'stay' + route: AnyRoute +} + +export type RootRouteOptions< + TSearchValidator = undefined, + TRouterContext = {}, + TRouteContextFn = AnyContext, + TBeforeLoadFn = AnyContext, + TLoaderDeps extends Record = {}, + TLoaderFn = undefined, +> = Omit< + RouteOptions< + any, // TParentRoute + RootRouteId, // TId + RootRouteId, // TCustomId + '', // TFullPath + '', // TPath + TSearchValidator, + {}, // TParams + TLoaderDeps, + TLoaderFn, + TRouterContext, + TRouteContextFn, + TBeforeLoadFn + >, + | 'path' + | 'id' + | 'getParentRoute' + | 'caseSensitive' + | 'parseParams' + | 'stringifyParams' + | 'params' +> + /** * @deprecated Use `ErrorComponentProps` instead. */ diff --git a/packages/router-core/src/typePrimitives.ts b/packages/router-core/src/typePrimitives.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/solid-router/src/Matches.tsx b/packages/solid-router/src/Matches.tsx index f9fdc8b31b9..b51d04d85fa 100644 --- a/packages/solid-router/src/Matches.tsx +++ b/packages/solid-router/src/Matches.tsx @@ -7,111 +7,29 @@ import { Transitioner } from './Transitioner' import { matchContext } from './matchContext' import { Match } from './Match' import { SafeFragment } from './SafeFragment' -import type { AnyRoute } from './route' import type { - AllContext, - AllLoaderData, - AllParams, - ControlledPromise, DeepPartial, - FullSearchSchema, MakeOptionalPathParams, MakeOptionalSearchParams, + MakeRouteMatchUnion, MaskOptions, NoInfer, - ParseRoute, ResolveRelativePath, ResolveRoute, - RouteById, RouteByPath, - RouteIds, - StaticDataRouteOption, ToSubOptionsProps, } from '@tanstack/router-core' import type { AnyRouter, RegisteredRouter, RouterState } from './router' -export type MakeRouteMatchFromRoute = RouteMatch< - TRoute['types']['id'], - TRoute['types']['fullPath'], - TRoute['types']['allParams'], - TRoute['types']['fullSearchSchema'], - TRoute['types']['loaderData'], - TRoute['types']['allContext'], - TRoute['types']['loaderDeps'] -> - -export interface RouteMatch< - out TRouteId, - out TFullPath, - out TAllParams, - out TFullSearchSchema, - out TLoaderData, - out TAllContext, - out TLoaderDeps, -> { - id: string - routeId: TRouteId - fullPath: TFullPath - index: number - pathname: string - params: TAllParams - _strictParams: TAllParams - status: 'pending' | 'success' | 'error' | 'redirected' | 'notFound' - isFetching: false | 'beforeLoad' | 'loader' - error: unknown - paramsError: unknown - searchError: unknown - updatedAt: number - loadPromise?: ControlledPromise - beforeLoadPromise?: ControlledPromise - loaderPromise?: ControlledPromise - loaderData?: TLoaderData - __routeContext: Record - __beforeLoadContext: Record - context: TAllContext - search: TFullSearchSchema - _strictSearch: TFullSearchSchema - fetchCount: number - abortController: AbortController - cause: 'preload' | 'enter' | 'stay' - loaderDeps: TLoaderDeps - preload: boolean - invalid: boolean - meta?: Array - links?: Array - scripts?: Array - headScripts?: Array - headers?: Record - globalNotFound?: boolean - staticData: StaticDataRouteOption - minPendingPromise?: ControlledPromise - pendingTimeout?: ReturnType +declare module '@tanstack/router-core' { + export interface RouteMatchExtensions { + meta?: Array + links?: Array + scripts?: Array + headScripts?: Array + } } -export type MakeRouteMatch< - TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], - TRouteId = RouteIds, - TStrict extends boolean = true, -> = RouteMatch< - TRouteId, - RouteById['types']['fullPath'], - TStrict extends false - ? AllParams - : RouteById['types']['allParams'], - TStrict extends false - ? FullSearchSchema - : RouteById['types']['fullSearchSchema'], - TStrict extends false - ? AllLoaderData - : RouteById['types']['loaderData'], - TStrict extends false - ? AllContext - : RouteById['types']['allContext'], - RouteById['types']['loaderDeps'] -> - -export type AnyRouteMatch = RouteMatch - export function Matches() { const router = useRouter() @@ -266,21 +184,6 @@ export function MatchRoute< ) } -export type MakeRouteMatchUnion< - TRouter extends AnyRouter = RegisteredRouter, - TRoute extends AnyRoute = ParseRoute, -> = TRoute extends any - ? RouteMatch< - TRoute['id'], - TRoute['fullPath'], - TRoute['types']['allParams'], - TRoute['types']['fullSearchSchema'], - TRoute['types']['loaderData'], - TRoute['types']['allContext'], - TRoute['types']['loaderDeps'] - > - : never - export interface UseMatchesBaseOptions { select?: (matches: Array>) => TSelected } diff --git a/packages/solid-router/src/fileRoute.ts b/packages/solid-router/src/fileRoute.ts index dfaa88a77db..a1766178372 100644 --- a/packages/solid-router/src/fileRoute.ts +++ b/packages/solid-router/src/fileRoute.ts @@ -18,20 +18,16 @@ import type { AnyValidator, Constrain, ConstrainLiteral, + FileBaseRouteOptions, FileRoutesByPath, ResolveParams, RouteById, RouteIds, -} from '@tanstack/router-core' - -import type { - AnyRoute, - FileBaseRouteOptions, - Route, - RouteConstraints, RouteLoaderFn, UpdatableRouteOptions, -} from './route' +} from '@tanstack/router-core' + +import type { AnyRoute, Route, RouteConstraints } from './route' import type { RegisteredRouter } from './router' import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseLoaderDataRoute } from './useLoaderData' diff --git a/packages/solid-router/src/index.tsx b/packages/solid-router/src/index.tsx index e3ff1a97c77..9c8c4851124 100644 --- a/packages/solid-router/src/index.tsx +++ b/packages/solid-router/src/index.tsx @@ -169,6 +169,18 @@ export type { AnyRedirect, Redirect, ResolvedRedirect, + RouteOptions, + FileBaseRouteOptions, + BaseRouteOptions, + UpdatableRouteOptions, + RouteLoaderFn, + LoaderFnContext, + RouteContextFn, + RouteContextOptions, + BeforeLoadFn, + BeforeLoadContextOptions, + ContextOptions, + RootRouteOptions, } from '@tanstack/router-core' export { @@ -225,13 +237,9 @@ export { } from './Matches' export type { - RouteMatch, - AnyRouteMatch, MatchRouteOptions, UseMatchRouteOptions, MakeMatchRouteOptions, - MakeRouteMatch, - MakeRouteMatchUnion, } from './Matches' export { matchContext } from './matchContext' @@ -256,12 +264,6 @@ export { NotFoundRoute, } from './route' export type { - RouteOptions, - FileBaseRouteOptions, - BaseRouteOptions, - UpdatableRouteOptions, - RouteLoaderFn, - LoaderFnContext, AnyRoute, RouteConstraints, AnyRootRoute, @@ -273,13 +275,7 @@ export type { RouteComponent, ErrorRouteComponent, NotFoundRouteComponent, - RootRouteOptions, AnyRouteWithContext, - RouteContextFn, - RouteContextOptions, - BeforeLoadFn, - BeforeLoadContextOptions, - ContextOptions, } from './route' export { diff --git a/packages/solid-router/src/route.ts b/packages/solid-router/src/route.ts index 7b620645b6b..6e8b94507d3 100644 --- a/packages/solid-router/src/route.ts +++ b/packages/solid-router/src/route.ts @@ -9,48 +9,29 @@ import { useNavigate } from './useNavigate' import { useMatch } from './useMatch' import type { AnyContext, - AnyPathParams, AnySchema, - AnyValidator, - BeforeLoadContextParameter, - BuildLocationFn, Constrain, ConstrainLiteral, RootRoute as CoreRootRoute, Route as CoreRoute, - DefaultValidator, ErrorComponentProps, - Expand, - FullSearchSchemaOption, - NavigateFn, - NavigateOptions, - NoInfer, NotFoundRouteProps, - ParamsOptions, - ParsedLocation, - RemountDepsOptions, - ResolveAllContext, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveFullSearchSchemaInput, ResolveId, - ResolveLoaderData, ResolveParams, RootRouteId, + RootRouteOptions, RouteById, RouteContext, - RouteContextParameter, RouteIds, - RoutePathOptions, + RouteLoaderFn, + RouteOptions, RoutePathOptionsIntersection, RoutePaths, RoutePrefix, RouteTypes, - SearchFilter, - SearchMiddleware, ToMaskOptions, TrimPathRight, - UpdatableStaticRouteOption, + UpdatableRouteOptions, UseNavigateResult, } from '@tanstack/router-core' import type { UseLoaderDataRoute } from './useLoaderData' @@ -59,488 +40,18 @@ import type { UseLoaderDepsRoute } from './useLoaderDeps' import type { UseParamsRoute } from './useParams' import type { UseSearchRoute } from './useSearch' import type * as Solid from 'solid-js' -import type { - AnyRouteMatch, - MakeRouteMatchFromRoute, - MakeRouteMatchUnion, - RouteMatch, -} from './Matches' import type { AnyRouter, RegisteredRouter, Router } from './router' import type { NotFoundError } from './not-found' import type { LazyRoute } from './fileRoute' import type { UseRouteContextRoute } from './useRouteContext' -export type RouteOptions< - TParentRoute extends AnyRoute = AnyRoute, - TId extends string = string, - TCustomId extends string = string, - TFullPath extends string = string, - TPath extends string = string, - TSearchValidator = undefined, - TParams = AnyPathParams, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, -> = BaseRouteOptions< - TParentRoute, - TId, - TCustomId, - TPath, - TSearchValidator, - TParams, - TLoaderDeps, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn -> & - UpdatableRouteOptions< - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer - > - -export type RouteContextFn< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, - in out TParams, - in out TRouterContext, -> = ( - ctx: RouteContextOptions< - TParentRoute, - TSearchValidator, - TParams, - TRouterContext - >, -) => any - -export type BeforeLoadFn< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, - in out TParams, - in out TRouterContext, - in out TRouteContextFn, -> = ( - ctx: BeforeLoadContextOptions< - TParentRoute, - TSearchValidator, - TParams, - TRouterContext, - TRouteContextFn - >, -) => any - -export type FileBaseRouteOptions< - TParentRoute extends AnyRoute = AnyRoute, - TId extends string = string, - TPath extends string = string, - TSearchValidator = undefined, - TParams = {}, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, - TRemountDepsFn = AnyContext, -> = ParamsOptions & { - validateSearch?: Constrain - - shouldReload?: - | boolean - | (( - match: LoaderFnContext< - TParentRoute, - TId, - TParams, - TLoaderDeps, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - ) => any) - - context?: Constrain< - TRouteContextFn, - ( - ctx: RouteContextOptions< - TParentRoute, - TParams, - TRouterContext, - TLoaderDeps - >, - ) => any - > - - // This async function is called before a route is loaded. - // If an error is thrown here, the route's loader will not be called. - // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function. - // If thrown during a preload event, the error will be logged to the console. - beforeLoad?: Constrain< - TBeforeLoadFn, - ( - ctx: BeforeLoadContextOptions< - TParentRoute, - TSearchValidator, - TParams, - TRouterContext, - TRouteContextFn - >, - ) => any - > - - loaderDeps?: ( - opts: FullSearchSchemaOption, - ) => TLoaderDeps - - remountDeps?: Constrain< - TRemountDepsFn, - ( - opt: RemountDepsOptions< - TId, - FullSearchSchemaOption, - Expand>, - TLoaderDeps - >, - ) => any - > - - loader?: Constrain< - TLoaderFn, - ( - ctx: LoaderFnContext< - TParentRoute, - TId, - TParams, - TLoaderDeps, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - ) => any - > -} - -export type BaseRouteOptions< - TParentRoute extends AnyRoute = AnyRoute, - TId extends string = string, - TCustomId extends string = string, - TPath extends string = string, - TSearchValidator = undefined, - TParams = {}, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, -> = RoutePathOptions & - FileBaseRouteOptions< - TParentRoute, - TId, - TPath, - TSearchValidator, - TParams, - TLoaderDeps, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - > & { - getParentRoute: () => TParentRoute - } - -export interface ContextOptions< - in out TParentRoute extends AnyRoute, - in out TParams, -> { - abortController: AbortController - preload: boolean - params: Expand> - location: ParsedLocation - /** - * @deprecated Use `throw redirect({ to: '/somewhere' })` instead - **/ - navigate: NavigateFn - buildLocation: BuildLocationFn - cause: 'preload' | 'enter' | 'stay' - matches: Array -} - -export interface RouteContextOptions< - in out TParentRoute extends AnyRoute, - in out TParams, - in out TRouterContext, - in out TLoaderDeps, -> extends ContextOptions { - deps: TLoaderDeps - context: Expand> -} - -export interface BeforeLoadContextOptions< - in out TParentRoute extends AnyRoute, - in out TSearchValidator, - in out TParams, - in out TRouterContext, - in out TRouteContextFn, -> extends ContextOptions, - FullSearchSchemaOption { - context: Expand< - BeforeLoadContextParameter - > -} - -type AssetFnContextOptions< - in out TRouteId, - in out TFullPath, - in out TParentRoute extends AnyRoute, - in out TParams, - in out TSearchValidator, - in out TLoaderFn, - in out TRouterContext, - in out TRouteContextFn, - in out TBeforeLoadFn, - in out TLoaderDeps, -> = { - matches: Array< - RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - > - > - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - > - params: ResolveAllParamsFromParent - loaderData: ResolveLoaderData -} - -export interface UpdatableRouteOptions< - in out TParentRoute extends AnyRoute, - in out TRouteId, - in out TFullPath, - in out TParams, - in out TSearchValidator, - in out TLoaderFn, - in out TLoaderDeps, - in out TRouterContext, - in out TRouteContextFn, - in out TBeforeLoadFn, -> extends UpdatableStaticRouteOption { - // If true, this route will be matched as case-sensitive - caseSensitive?: boolean - // If true, this route will be forcefully wrapped in a suspense boundary - wrapInSuspense?: boolean - // The content to be rendered when the route is matched. If no component is provided, defaults to `` - component?: RouteComponent - errorComponent?: false | null | ErrorRouteComponent - notFoundComponent?: NotFoundRouteComponent - pendingComponent?: RouteComponent - pendingMs?: number - pendingMinMs?: number - staleTime?: number - gcTime?: number - preload?: boolean - preloadStaleTime?: number - preloadGcTime?: number - search?: { - middlewares?: Array< - SearchMiddleware< - ResolveFullSearchSchemaInput - > - > - } - /** - @deprecated Use search.middlewares instead - */ - preSearchFilters?: Array< - SearchFilter> - > - /** - @deprecated Use search.middlewares instead - */ - postSearchFilters?: Array< - SearchFilter> - > - onCatch?: (error: Error) => void - onError?: (err: any) => void - // These functions are called as route matches are loaded, stick around and leave the active - // matches - onEnter?: ( - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - >, - ) => void - onStay?: ( - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - >, - ) => void - onLeave?: ( - match: RouteMatch< - TRouteId, - TFullPath, - ResolveAllParamsFromParent, - ResolveFullSearchSchema, - ResolveLoaderData, - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - TLoaderDeps - >, - ) => void - headers?: (ctx: { - loaderData: ResolveLoaderData - }) => Record - head?: ( - ctx: AssetFnContextOptions< - TRouteId, - TFullPath, - TParentRoute, - TParams, - TSearchValidator, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, - TLoaderDeps - >, - ) => { - links?: AnyRouteMatch['links'] - scripts?: AnyRouteMatch['headScripts'] - meta?: AnyRouteMatch['meta'] +declare module '@tanstack/router-core' { + export interface UpdatableRouteOptionsExtensions { + component?: RouteComponent + errorComponent?: false | null | ErrorRouteComponent + notFoundComponent?: NotFoundRouteComponent + pendingComponent?: RouteComponent } - scripts?: ( - ctx: AssetFnContextOptions< - TRouteId, - TFullPath, - TParentRoute, - TParams, - TSearchValidator, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn, - TLoaderDeps - >, - ) => AnyRouteMatch['scripts'] - ssr?: boolean - codeSplitGroupings?: Array< - Array< - | 'loader' - | 'component' - | 'pendingComponent' - | 'notFoundComponent' - | 'errorComponent' - > - > -} - -export type RouteLoaderFn< - in out TParentRoute extends AnyRoute = AnyRoute, - in out TId extends string = string, - in out TParams = {}, - in out TLoaderDeps = {}, - in out TRouterContext = {}, - in out TRouteContextFn = AnyContext, - in out TBeforeLoadFn = AnyContext, -> = ( - match: LoaderFnContext< - TParentRoute, - TId, - TParams, - TLoaderDeps, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, -) => any - -export interface LoaderFnContext< - in out TParentRoute extends AnyRoute = AnyRoute, - in out TId extends string = string, - in out TParams = {}, - in out TLoaderDeps = {}, - in out TRouterContext = {}, - in out TRouteContextFn = AnyContext, - in out TBeforeLoadFn = AnyContext, -> { - abortController: AbortController - preload: boolean - params: Expand> - deps: TLoaderDeps - context: Expand< - ResolveAllContext< - TParentRoute, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - > - > - location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps - /** - * @deprecated Use `throw redirect({ to: '/somewhere' })` instead - **/ - navigate: (opts: NavigateOptions) => Promise | void - // root route does not have a parent match - parentMatchPromise: TId extends RootRouteId - ? never - : Promise> - cause: 'preload' | 'enter' | 'stay' - route: Route } export interface AnyRoute @@ -1112,37 +623,6 @@ export function createRoute< export type AnyRootRoute = RootRoute -export type RootRouteOptions< - TSearchValidator = undefined, - TRouterContext = {}, - TRouteContextFn = AnyContext, - TBeforeLoadFn = AnyContext, - TLoaderDeps extends Record = {}, - TLoaderFn = undefined, -> = Omit< - RouteOptions< - any, // TParentRoute - RootRouteId, // TId - RootRouteId, // TCustomId - '', // TFullPath - '', // TPath - TSearchValidator, - {}, // TParams - TLoaderDeps, - TLoaderFn, - TRouterContext, - TRouteContextFn, - TBeforeLoadFn - >, - | 'path' - | 'id' - | 'getParentRoute' - | 'caseSensitive' - | 'parseParams' - | 'stringifyParams' - | 'params' -> - export function createRootRouteWithContext() { return < TRouteContextFn = AnyContext, diff --git a/packages/solid-router/src/router.ts b/packages/solid-router/src/router.ts index c379c6ebcf3..0f2472ccef0 100644 --- a/packages/solid-router/src/router.ts +++ b/packages/solid-router/src/router.ts @@ -41,13 +41,18 @@ import type { NoInfer } from '@tanstack/solid-store' import type { AnyContext, AnyRedirect, + AnyRouteMatch, AnySchema, AnyValidator, + BeforeLoadContextOptions, BuildLocationFn, CommitLocationOptions, ControlledPromise, FullSearchSchema, + LoaderFnContext, MakeRemountDepsOptionsUnion, + MakeRouteMatch, + MakeRouteMatchUnion, Manifest, NavigateFn, NavigateOptions, @@ -58,6 +63,7 @@ import type { ResolveRelativePath, ResolvedRedirect, RouteById, + RouteContextOptions, RoutePaths, RoutesById, RoutesByPath, @@ -72,21 +78,13 @@ import type { } from '@tanstack/router-core' import type { AnyRoute, - BeforeLoadContextOptions, ErrorRouteComponent, - LoaderFnContext, NotFoundRouteComponent, RootRoute, RouteComponent, - RouteContextOptions, RouteMask, } from './route' -import type { - AnyRouteMatch, - MakeRouteMatch, - MakeRouteMatchUnion, - MatchRouteOptions, -} from './Matches' +import type { MatchRouteOptions } from './Matches' import type { NotFoundError } from './not-found' diff --git a/packages/solid-router/src/useMatch.tsx b/packages/solid-router/src/useMatch.tsx index 5c45b1145c1..3223c4823f2 100644 --- a/packages/solid-router/src/useMatch.tsx +++ b/packages/solid-router/src/useMatch.tsx @@ -3,9 +3,12 @@ import invariant from 'tiny-invariant' import { useRouterState } from './useRouterState' import { dummyMatchContext, matchContext } from './matchContext' import type { AnyRouter, RegisteredRouter } from './router' -import type { MakeRouteMatch, MakeRouteMatchUnion } from './Matches' import type { StrictOrFrom } from './utils' -import type { ThrowOrOptional } from '@tanstack/router-core' +import type { + MakeRouteMatch, + MakeRouteMatchUnion, + ThrowOrOptional, +} from '@tanstack/router-core' export interface UseMatchBaseOptions< TRouter extends AnyRouter, From 257bc0f97bc08b523c54f55f23816bb237a8b880 Mon Sep 17 00:00:00 2001 From: chorobin Date: Mon, 24 Feb 2025 22:55:59 +0100 Subject: [PATCH 8/8] chore: fix build --- packages/react-router/src/index.tsx | 2 +- packages/react-router/tests/route.test-d.tsx | 30 ++++++++++--------- .../react-router/tests/useMatch.test-d.tsx | 2 +- packages/router-core/src/route.ts | 8 ++++- .../solid-router/tests/Matches.test-d.tsx | 2 +- packages/solid-router/tests/route.test-d.tsx | 26 ++++++++-------- .../solid-router/tests/routeApi.test-d.tsx | 2 +- .../solid-router/tests/useMatch.test-d.tsx | 2 +- 8 files changed, 41 insertions(+), 33 deletions(-) diff --git a/packages/react-router/src/index.tsx b/packages/react-router/src/index.tsx index f435ead4972..e782caabbcf 100644 --- a/packages/react-router/src/index.tsx +++ b/packages/react-router/src/index.tsx @@ -123,6 +123,7 @@ export type { ValidatorObj, FileRoutesByPath, RouteById, + RootRouteOptions, } from '@tanstack/router-core' export { @@ -271,7 +272,6 @@ export type { RouteComponent, ErrorRouteComponent, NotFoundRouteComponent, - RootRouteOptions, } from './route' export { diff --git a/packages/react-router/tests/route.test-d.tsx b/packages/react-router/tests/route.test-d.tsx index fd7311de6fd..2183cf4af71 100644 --- a/packages/react-router/tests/route.test-d.tsx +++ b/packages/react-router/tests/route.test-d.tsx @@ -7,19 +7,20 @@ import { redirect, } from '../src' import type { - AnyRouter, BuildLocationFn, ControlledPromise, NavigateFn, NavigateOptions, - ParsedLocation, - Route, SearchSchemaInput, } from '../src' import type { + AnyRoute, + AnyRouter, + Expand, MakeRouteMatchFromRoute, MakeRouteMatchUnion, -} from '../src/Matches' + ParsedLocation, +} from '@tanstack/router-core' test('when creating the root', () => { const rootRoute = createRootRoute() @@ -88,7 +89,7 @@ test('when creating the root with a loader', () => { navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: never cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -194,7 +195,7 @@ test('when creating the root route with context and a loader', () => { navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: never cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -258,6 +259,7 @@ test('when creating the root route with context, routeContext, beforeLoad and a return { permission: 'view' as const } }, loader: (opts) => { + type hi = Expand expectTypeOf(opts).toEqualTypeOf<{ abortController: AbortController preload: boolean @@ -268,7 +270,7 @@ test('when creating the root route with context, routeContext, beforeLoad and a navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: never cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -408,7 +410,7 @@ test('when creating a child route with a loader from the root route', () => { navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() return [{ id: 'invoice1' }, { id: 'invoice2' }] as const }, @@ -459,7 +461,7 @@ test('when creating a child route with a loader from the root route with context navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() return [{ id: 'invoice1' }, { id: 'invoice2' }] as const }, @@ -679,7 +681,7 @@ test('when creating a child route with params, search and loader from the root r navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }> }, }) @@ -704,7 +706,7 @@ test('when creating a child route with params, search, loader and loaderDeps fro navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>(), }) }) @@ -728,7 +730,7 @@ test('when creating a child route with params, search, loader and loaderDeps fro navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>(), }) }) @@ -831,7 +833,7 @@ test('when creating a child route with params, search with routeContext, beforeL navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -1200,7 +1202,7 @@ test('when creating a child route with routeContext, beforeLoad, search, params, MakeRouteMatchFromRoute > cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>(), }) }) diff --git a/packages/react-router/tests/useMatch.test-d.tsx b/packages/react-router/tests/useMatch.test-d.tsx index c282a69d0c4..7b6efbbd6e7 100644 --- a/packages/react-router/tests/useMatch.test-d.tsx +++ b/packages/react-router/tests/useMatch.test-d.tsx @@ -1,6 +1,6 @@ import { describe, expectTypeOf, test } from 'vitest' import { createRootRoute, createRoute, createRouter, useMatch } from '../src' -import type { MakeRouteMatch, MakeRouteMatchUnion } from '../src/Matches' +import type { MakeRouteMatch, MakeRouteMatchUnion } from '@tanstack/router-core' const rootRoute = createRootRoute() diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index ac2d17747de..72a7ca63f91 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -10,7 +10,13 @@ import type { RootRouteId } from './root' import type { ParseRoute } from './routeInfo' import type { AnyRouter, RegisteredRouter } from './router' import type { BuildLocationFn, NavigateFn } from './RouterProvider' -import type { Assign, Constrain, Expand, IntersectAssign } from './utils' +import type { + Assign, + Constrain, + Expand, + IntersectAssign, + NoInfer, +} from './utils' import type { AnySchema, AnyStandardSchemaValidator, diff --git a/packages/solid-router/tests/Matches.test-d.tsx b/packages/solid-router/tests/Matches.test-d.tsx index 4999f799ed7..b107435db62 100644 --- a/packages/solid-router/tests/Matches.test-d.tsx +++ b/packages/solid-router/tests/Matches.test-d.tsx @@ -8,7 +8,7 @@ import { useMatchRoute, useMatches, } from '../src' -import type { AnyRouteMatch, RouteMatch } from '../src' +import type { AnyRouteMatch, RouteMatch } from '@tanstack/router-core' import type * as Solid from 'solid-js' const rootRoute = createRootRoute() diff --git a/packages/solid-router/tests/route.test-d.tsx b/packages/solid-router/tests/route.test-d.tsx index 48e784183cf..fc58b779ab6 100644 --- a/packages/solid-router/tests/route.test-d.tsx +++ b/packages/solid-router/tests/route.test-d.tsx @@ -8,19 +8,19 @@ import { } from '../src' import type { Accessor } from 'solid-js' import type { - AnyRouter, BuildLocationFn, ControlledPromise, NavigateFn, NavigateOptions, ParsedLocation, - Route, SearchSchemaInput, } from '../src' import type { + AnyRoute, + AnyRouter, MakeRouteMatchFromRoute, MakeRouteMatchUnion, -} from '../src/Matches' +} from '@tanstack/router-core' test('when creating the root', () => { const rootRoute = createRootRoute() @@ -89,7 +89,7 @@ test('when creating the root with a loader', () => { navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: never cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -197,7 +197,7 @@ test('when creating the root route with context and a loader', () => { navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: never cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -272,7 +272,7 @@ test('when creating the root route with context, routeContext, beforeLoad and a navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: never cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -414,7 +414,7 @@ test('when creating a child route with a loader from the root route', () => { navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() return [{ id: 'invoice1' }, { id: 'invoice2' }] as const }, @@ -460,7 +460,7 @@ test('when creating a child route with a loader from the root route with context navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() return [{ id: 'invoice1' }, { id: 'invoice2' }] as const }, @@ -654,7 +654,7 @@ test('when creating a child route with params, search and loader from the root r navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }> }, }) @@ -679,7 +679,7 @@ test('when creating a child route with params, search, loader and loaderDeps fro navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>(), }) }) @@ -703,7 +703,7 @@ test('when creating a child route with params, search, loader and loaderDeps fro navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>(), }) }) @@ -806,7 +806,7 @@ test('when creating a child route with params, search with routeContext, beforeL navigate: (opts: NavigateOptions) => Promise | void parentMatchPromise: Promise> cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>() }, }) @@ -1167,7 +1167,7 @@ test('when creating a child route with routeContext, beforeLoad, search, params, MakeRouteMatchFromRoute > cause: 'preload' | 'enter' | 'stay' - route: Route + route: AnyRoute }>(), }) }) diff --git a/packages/solid-router/tests/routeApi.test-d.tsx b/packages/solid-router/tests/routeApi.test-d.tsx index 34d56729bd3..99908a3d7a4 100644 --- a/packages/solid-router/tests/routeApi.test-d.tsx +++ b/packages/solid-router/tests/routeApi.test-d.tsx @@ -1,7 +1,7 @@ import { describe, expectTypeOf, test } from 'vitest' import { createRootRoute, createRoute, createRouter, getRouteApi } from '../src' import type { Accessor } from 'solid-js' -import type { MakeRouteMatch, UseNavigateResult } from '../src' +import type { MakeRouteMatch, UseNavigateResult } from '@tanstack/router-core' const rootRoute = createRootRoute() diff --git a/packages/solid-router/tests/useMatch.test-d.tsx b/packages/solid-router/tests/useMatch.test-d.tsx index e2fba8547fe..8d40d4caa5d 100644 --- a/packages/solid-router/tests/useMatch.test-d.tsx +++ b/packages/solid-router/tests/useMatch.test-d.tsx @@ -1,6 +1,6 @@ import { describe, expectTypeOf, test } from 'vitest' import { createRootRoute, createRoute, createRouter, useMatch } from '../src' -import type { MakeRouteMatch, MakeRouteMatchUnion } from '../src/Matches' +import type { MakeRouteMatch, MakeRouteMatchUnion } from '@tanstack/router-core' import type * as Solid from 'solid-js' const rootRoute = createRootRoute()