Open History to see the last commit
1import type { AnyRoute, StaticDataRouteOption } from './route'
2import type {
3 AllContext,
4 AllLoaderData,
5 AllParams,
6 FullSearchSchema,
7 ParseRoute,
8 RouteById,
9 RouteIds,
10} from './routeInfo'
11import type { AnyRouter, RegisteredRouter, SSROption } from './router'
12import type { Constrain } from './utils'
13β
14export type AnyMatchAndValue = { match: any; value: any }
15β
16export type FindValueByIndex<
17 TKey,
18 TValue extends ReadonlyArray<any>,
19> = TKey extends `${infer TIndex extends number}` ? TValue[TIndex] : never
20β
21export type FindValueByKey<TKey, TValue> =
22 TValue extends ReadonlyArray<any>
23 ? FindValueByIndex<TKey, TValue>
24 : TValue[TKey & keyof TValue]
25β
26export type CreateMatchAndValue<TMatch, TValue> = TValue extends any
27 ? {
28 match: TMatch
29 value: TValue
30 }
31 : never
32β
33export type NextMatchAndValue<
34 TKey,
35 TMatchAndValue extends AnyMatchAndValue,
36> = TMatchAndValue extends any
37 ? CreateMatchAndValue<
38 TMatchAndValue['match'],
39 FindValueByKey<TKey, TMatchAndValue['value']>
40 >
41 : never
42β
43export type IsMatchKeyOf<TValue> =
44 TValue extends ReadonlyArray<any>
45 ? number extends TValue['length']
46 ? `${number}`
47 : keyof TValue & `${number}`
48 : TValue extends object
49 ? keyof TValue & string
50 : never
51β
52export type IsMatchPath<
53 TParentPath extends string,
54 TMatchAndValue extends AnyMatchAndValue,
55> = `${TParentPath}${IsMatchKeyOf<TMatchAndValue['value']>}`
56β
57export type IsMatchResult<
58 TKey,
59 TMatchAndValue extends AnyMatchAndValue,
60> = TMatchAndValue extends any
61 ? TKey extends keyof TMatchAndValue['value']
62 ? TMatchAndValue['match']
63 : never
64 : never
65β
66export type IsMatchParse<
67 TPath,
68 TMatchAndValue extends AnyMatchAndValue,
69 TParentPath extends string = '',
70> = TPath extends `${string}.${string}`
71 ? TPath extends `${infer TFirst}.${infer TRest}`
72 ? IsMatchParse<
73 TRest,
74 NextMatchAndValue<TFirst, TMatchAndValue>,
75 `${TParentPath}${TFirst}.`
76 >
77 : never
78 : {
79 path: IsMatchPath<TParentPath, TMatchAndValue>
80 result: IsMatchResult<TPath, TMatchAndValue>
81 }
82β
83export type IsMatch<TMatch, TPath> = IsMatchParse<
84 TPath,
85 TMatch extends any ? { match: TMatch; value: TMatch } : never
86>
87β
88/**
89 * Narrows matches based on a path
90 * @experimental
91 */
92export const isMatch = <TMatch, TPath extends string>(
93 match: TMatch,
94 path: Constrain<TPath, IsMatch<TMatch, TPath>['path']>,
95): match is IsMatch<TMatch, TPath>['result'] => {
96 const parts = (path as string).split('.')
97 let part
98 let i = 0
99 let value: any = match
100β
101 while ((part = parts[i++]) != null && value != null) {
102 value = value[part]
103 }
104β
105 return value != null
106}
107β
108export interface DefaultRouteMatchExtensions {
109 scripts?: unknown
110 links?: unknown
111 headScripts?: unknown
112 meta?: unknown
113 styles?: unknown
114}
115β
116export interface RouteMatchExtensions extends DefaultRouteMatchExtensions {}
117β
118export interface RouteMatch<
119 out TRouteId,
120 out TFullPath,
121 out TAllParams,
122 out TFullSearchSchema,
123 out TLoaderData,
124 out TAllContext,
125 out TLoaderDeps,
126> extends RouteMatchExtensions {
127 id: string
128 routeId: TRouteId
129 fullPath: TFullPath
130 index: number
131 pathname: string
132 params: TAllParams
133 _strictParams: TAllParams
134 status: 'pending' | 'success' | 'error' | 'notFound'
135 isFetching: false | 'beforeLoad' | 'loader'
136 error: unknown
137 paramsError: unknown
138 searchError: unknown
139 updatedAt: number
140 loaderData?: TLoaderData
141 /** @internal Exclusive end of the SSR-verified asset prefix. */
142 _assetEnd?: number
143 /** @internal */
144 _ctx?: Record<string, unknown>
145 /** @internal */
146 __beforeLoadContext?: Record<string, unknown>
147 context: TAllContext
148 search: TFullSearchSchema
149 _strictSearch: TFullSearchSchema
150 abortController: AbortController
151 cause: 'preload' | 'enter' | 'stay'
152 loaderDeps: TLoaderDeps
153 preload: boolean
154 invalid: boolean
155 headers?: Record<string, string>
156 _notFound?: boolean
157 staticData: StaticDataRouteOption
158 /** This attribute is not reactive */
159 ssr?: SSROption
160}
161β
162export interface PreValidationErrorHandlingRouteMatch<
163 TRouteId,
164 TFullPath,
165 TAllParams,
166 TFullSearchSchema,
167> {
168 id: string
169 routeId: TRouteId
170 fullPath: TFullPath
171 index: number
172 pathname: string
173 search:
174 | { status: 'success'; value: TFullSearchSchema }
175 | { status: 'error'; error: unknown }
176 params:
177 | { status: 'success'; value: TAllParams }
178 | { status: 'error'; error: unknown }
179 staticData: StaticDataRouteOption
180 ssr?: boolean | 'data-only'
181}
182β
183export type MakePreValidationErrorHandlingRouteMatchUnion<
184 TRouter extends AnyRouter = RegisteredRouter,
185 TRoute extends AnyRoute = ParseRoute<TRouter['routeTree']>,
186> = TRoute extends any
187 ? PreValidationErrorHandlingRouteMatch<
188 TRoute['id'],
189 TRoute['fullPath'],
190 TRoute['types']['allParams'],
191 TRoute['types']['fullSearchSchema']
192 >
193 : never
194β
195export type MakeRouteMatchFromRoute<TRoute extends AnyRoute> = RouteMatch<
196 TRoute['types']['id'],
197 TRoute['types']['fullPath'],
198 TRoute['types']['allParams'],
199 TRoute['types']['fullSearchSchema'],
200 TRoute['types']['loaderData'],
201 TRoute['types']['allContext'],
202 TRoute['types']['loaderDeps']
203>
204β
205export type MakeRouteMatch<
206 TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
207 TRouteId = RouteIds<TRouteTree>,
208 TStrict extends boolean = true,
209> = RouteMatch<
210 TRouteId,
211 RouteById<TRouteTree, TRouteId>['types']['fullPath'],
212 TStrict extends false
213 ? AllParams<TRouteTree>
214 : RouteById<TRouteTree, TRouteId>['types']['allParams'],
215 TStrict extends false
216 ? FullSearchSchema<TRouteTree>
217 : RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema'],
218 TStrict extends false
219 ? AllLoaderData<TRouteTree>
220 : RouteById<TRouteTree, TRouteId>['types']['loaderData'],
221 TStrict extends false
222 ? AllContext<TRouteTree>
223 : RouteById<TRouteTree, TRouteId>['types']['allContext'],
224 RouteById<TRouteTree, TRouteId>['types']['loaderDeps']
225>
226β
227export type AnyRouteMatch = RouteMatch<any, any, any, any, any, any, any>
228β
229export type MakeRouteMatchUnion<
230 TRouter extends AnyRouter = RegisteredRouter,
231 TRoute extends AnyRoute = ParseRoute<TRouter['routeTree']>,
232> = TRoute extends any
233 ? RouteMatch<
234 TRoute['id'],
235 TRoute['fullPath'],
236 TRoute['types']['allParams'],
237 TRoute['types']['fullSearchSchema'],
238 TRoute['types']['loaderData'],
239 TRoute['types']['allContext'],
240 TRoute['types']['loaderDeps']
241 >
242 : never
243β
244/**
245 * The `MatchRouteOptions` type is used to describe the options that can be used when matching a route.
246 *
247 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#matchrouteoptions-type)
248 */
249export interface MatchRouteOptions {
250 /**
251 * If `true`, will match against pending location instead of the current location.
252 *
253 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#pending-property)
254 */
255 pending?: boolean
256 /**
257 * If `true`, will match against the current location with case sensitivity.
258 *
259 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#casesensitive-property)
260 *
261 * @deprecated Declare case sensitivity in the route definition instead, or globally for all routes using the `caseSensitive` option on the router.
262 */
263 caseSensitive?: boolean
264 /**
265 * If `true`, will match against the current location's search params using a deep inclusive check. e.g. `{ a: 1 }` will match for a current location of `{ a: 1, b: 2 }`.
266 *
267 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#includesearch-property)
268 */
269 includeSearch?: boolean
270 /**
271 * If `true`, will match against the current location using a fuzzy match. e.g. `/posts` will match for a current location of `/posts/123`.
272 *
273 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#fuzzy-property)
274 */
275 fuzzy?: boolean
276}