@tanstack/react-router 1.27.0 → 1.28.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/cjs/Matches.cjs +45 -23
  2. package/dist/cjs/Matches.cjs.map +1 -1
  3. package/dist/cjs/Matches.d.cts +5 -6
  4. package/dist/cjs/fileRoute.cjs.map +1 -1
  5. package/dist/cjs/fileRoute.d.cts +13 -14
  6. package/dist/cjs/index.cjs +0 -1
  7. package/dist/cjs/index.cjs.map +1 -1
  8. package/dist/cjs/index.d.cts +2 -2
  9. package/dist/cjs/link.cjs.map +1 -1
  10. package/dist/cjs/link.d.cts +34 -24
  11. package/dist/cjs/redirects.cjs.map +1 -1
  12. package/dist/cjs/redirects.d.cts +3 -1
  13. package/dist/cjs/route.cjs.map +1 -1
  14. package/dist/cjs/route.d.cts +44 -47
  15. package/dist/cjs/routeInfo.d.cts +5 -7
  16. package/dist/cjs/router.cjs +374 -327
  17. package/dist/cjs/router.cjs.map +1 -1
  18. package/dist/cjs/router.d.cts +3 -3
  19. package/dist/cjs/useParams.cjs.map +1 -1
  20. package/dist/cjs/utils.cjs +20 -2
  21. package/dist/cjs/utils.cjs.map +1 -1
  22. package/dist/cjs/utils.d.cts +11 -4
  23. package/dist/esm/Matches.d.ts +5 -6
  24. package/dist/esm/Matches.js +46 -24
  25. package/dist/esm/Matches.js.map +1 -1
  26. package/dist/esm/fileRoute.d.ts +13 -14
  27. package/dist/esm/fileRoute.js.map +1 -1
  28. package/dist/esm/index.d.ts +2 -2
  29. package/dist/esm/index.js +1 -2
  30. package/dist/esm/link.d.ts +34 -24
  31. package/dist/esm/link.js.map +1 -1
  32. package/dist/esm/redirects.d.ts +3 -1
  33. package/dist/esm/redirects.js.map +1 -1
  34. package/dist/esm/route.d.ts +44 -47
  35. package/dist/esm/route.js.map +1 -1
  36. package/dist/esm/routeInfo.d.ts +5 -7
  37. package/dist/esm/router.d.ts +3 -3
  38. package/dist/esm/router.js +375 -328
  39. package/dist/esm/router.js.map +1 -1
  40. package/dist/esm/useParams.js.map +1 -1
  41. package/dist/esm/utils.d.ts +11 -4
  42. package/dist/esm/utils.js +20 -2
  43. package/dist/esm/utils.js.map +1 -1
  44. package/package.json +4 -2
  45. package/src/Matches.tsx +75 -37
  46. package/src/fileRoute.ts +17 -36
  47. package/src/index.tsx +0 -2
  48. package/src/link.tsx +189 -82
  49. package/src/redirects.ts +4 -2
  50. package/src/route.ts +149 -187
  51. package/src/routeInfo.ts +5 -7
  52. package/src/router.ts +501 -429
  53. package/src/useParams.tsx +2 -2
  54. package/src/utils.ts +41 -8
package/src/link.tsx CHANGED
@@ -3,6 +3,7 @@ import { useMatch } from './Matches'
3
3
  import { useRouterState } from './useRouterState'
4
4
  import { useRouter } from './useRouter'
5
5
  import { deepEqual, exactPathTest, functionalUpdate } from './utils'
6
+ import type { ParsedLocation } from '.'
6
7
  import type { HistoryState } from '@tanstack/history'
7
8
  import type { Trim } from './fileRoute'
8
9
  import type { AnyRoute, RootSearchSchema } from './route'
@@ -84,67 +85,78 @@ export type RemoveLeadingSlashes<T> = T extends `/${infer R}`
84
85
  ? RemoveLeadingSlashes<R>
85
86
  : T
86
87
 
88
+ export type ResolvePaths<TRouteTree extends AnyRoute, TSearchPath> =
89
+ RouteByPath<TRouteTree, RemoveTrailingSlashes<TSearchPath>> extends never
90
+ ? RoutePaths<TRouteTree>
91
+ : RoutePaths<RouteByPath<TRouteTree, RemoveTrailingSlashes<TSearchPath>>>
92
+
87
93
  export type SearchPaths<
88
- TPaths,
94
+ TRouteTree extends AnyRoute,
89
95
  TSearchPath extends string,
96
+ TPaths = ResolvePaths<TRouteTree, TSearchPath>,
90
97
  > = TPaths extends `${RemoveTrailingSlashes<TSearchPath>}/${infer TRest}`
91
98
  ? TRest
92
99
  : never
93
100
 
94
101
  export type SearchRelativePathAutoComplete<
102
+ TRouteTree extends AnyRoute,
95
103
  TTo extends string,
96
104
  TSearchPath extends string,
97
- TPaths,
98
- TSearchedPaths = SearchPaths<TPaths, TSearchPath>,
99
- > = TSearchedPaths extends string ? `${TTo}/${TSearchedPaths}` : never
105
+ > = `${TTo}/${SearchPaths<TRouteTree, TSearchPath>}`
100
106
 
101
107
  export type RelativeToParentPathAutoComplete<
108
+ TRouteTree extends AnyRoute,
102
109
  TFrom extends string,
103
110
  TTo extends string,
104
- TPaths,
105
111
  TResolvedPath extends string = RemoveTrailingSlashes<
106
112
  ResolveRelativePath<TFrom, TTo>
107
113
  >,
108
114
  > =
109
- | SearchRelativePathAutoComplete<TTo, TResolvedPath, TPaths>
115
+ | SearchRelativePathAutoComplete<TRouteTree, TTo, TResolvedPath>
110
116
  | (TResolvedPath extends '' ? never : `${TTo}/../`)
111
117
 
112
118
  export type RelativeToCurrentPathAutoComplete<
119
+ TRouteTree extends AnyRoute,
113
120
  TFrom extends string,
114
121
  TTo extends string,
115
122
  TRestTo extends string,
116
- TPaths,
117
123
  TResolvedPath extends
118
124
  string = RemoveTrailingSlashes<`${RemoveTrailingSlashes<TFrom>}/${RemoveLeadingSlashes<TRestTo>}`>,
119
- > = SearchRelativePathAutoComplete<TTo, TResolvedPath, TPaths>
125
+ > = SearchRelativePathAutoComplete<TRouteTree, TTo, TResolvedPath>
120
126
 
121
- export type AbsolutePathAutoComplete<TFrom extends string, TPaths> =
127
+ export type AbsolutePathAutoComplete<
128
+ TRouteTree extends AnyRoute,
129
+ TFrom extends string,
130
+ > =
122
131
  | (string extends TFrom
123
132
  ? './'
124
133
  : TFrom extends `/`
125
134
  ? never
126
- : SearchPaths<TPaths, TFrom> extends ''
135
+ : SearchPaths<TRouteTree, TFrom> extends ''
127
136
  ? never
128
137
  : './')
129
138
  | (string extends TFrom ? '../' : TFrom extends `/` ? never : '../')
130
- | TPaths
131
- | (TFrom extends '/' ? never : SearchPaths<TPaths, TFrom>)
139
+ | RoutePaths<TRouteTree>
140
+ | (TFrom extends '/' ? never : SearchPaths<TRouteTree, TFrom>)
132
141
 
133
142
  export type RelativeToPathAutoComplete<
134
143
  TRouteTree extends AnyRoute,
135
144
  TFrom extends string,
136
145
  TTo extends string,
137
- TPaths = RoutePaths<TRouteTree>,
138
146
  > = TTo extends `..${string}`
139
- ? RelativeToParentPathAutoComplete<TFrom, RemoveTrailingSlashes<TTo>, TPaths>
147
+ ? RelativeToParentPathAutoComplete<
148
+ TRouteTree,
149
+ TFrom,
150
+ RemoveTrailingSlashes<TTo>
151
+ >
140
152
  : TTo extends `./${infer TRestTTo}`
141
153
  ? RelativeToCurrentPathAutoComplete<
154
+ TRouteTree,
142
155
  TFrom,
143
156
  RemoveTrailingSlashes<TTo>,
144
- TRestTTo,
145
- TPaths
157
+ TRestTTo
146
158
  >
147
- : AbsolutePathAutoComplete<TFrom, TPaths>
159
+ : AbsolutePathAutoComplete<TRouteTree, TFrom>
148
160
 
149
161
  export type NavigateOptions<
150
162
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
@@ -167,6 +179,7 @@ export type ToOptions<
167
179
  TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
168
180
  TMaskTo extends string = '',
169
181
  > = ToSubOptions<TRouteTree, TFrom, TTo> & {
182
+ _fromLocation?: ParsedLocation
170
183
  mask?: ToMaskOptions<TRouteTree, TMaskFrom, TMaskTo>
171
184
  }
172
185
 
@@ -183,19 +196,19 @@ export type ToSubOptions<
183
196
  TFrom extends RoutePaths<TRouteTree> | string = string,
184
197
  TTo extends string = '',
185
198
  > = {
186
- to?: ToPathOption<TRouteTree, TFrom, TTo>
199
+ to?: ToPathOption<TRouteTree, TFrom, TTo> & {}
187
200
  hash?: true | Updater<string>
188
201
  state?: true | NonNullableUpdater<HistoryState>
189
202
  // The source route path. This is automatically set when using route-level APIs, but for type-safe relative routing on the router itself, this is required
190
- from?: RoutePathsAutoComplete<TRouteTree, TFrom>
203
+ from?: RoutePathsAutoComplete<TRouteTree, TFrom> & {}
191
204
  // // When using relative route paths, this option forces resolution from the current path, instead of the route API's path or `from` path
192
- } & CheckPath<TRouteTree, {}, TFrom, TTo> &
193
- SearchParamOptions<TRouteTree, TFrom, TTo> &
205
+ } & SearchParamOptions<TRouteTree, TFrom, TTo> &
194
206
  PathParamOptions<TRouteTree, TFrom, TTo>
195
207
 
196
208
  type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
197
209
 
198
210
  type ParamVariant = 'PATH' | 'SEARCH'
211
+
199
212
  type ExcludeRootSearchSchema<T, TExcluded = Exclude<T, RootSearchSchema>> = [
200
213
  TExcluded,
201
214
  ] extends [never]
@@ -224,49 +237,147 @@ type PostProcessParams<
224
237
  TParamVariant extends ParamVariant,
225
238
  > = TParamVariant extends 'SEARCH' ? ExcludeRootSearchSchema<T> : T
226
239
 
227
- export type ParamOptions<
240
+ type ResolveFromParams<
228
241
  TRouteTree extends AnyRoute,
229
- TFrom,
230
- TTo extends string,
231
242
  TParamVariant extends ParamVariant,
243
+ TFrom,
232
244
  TFromRouteType extends
233
245
  | 'allParams'
234
246
  | 'fullSearchSchema' = TParamVariant extends 'PATH'
235
247
  ? 'allParams'
236
248
  : 'fullSearchSchema',
237
- TToRouteType extends
238
- | 'allParams'
239
- | 'fullSearchSchemaInput' = TParamVariant extends 'PATH'
249
+ > = PostProcessParams<
250
+ RouteByPath<TRouteTree, TFrom>['types'][TFromRouteType],
251
+ TParamVariant
252
+ >
253
+
254
+ type ResolveToParams<
255
+ TRouteTree extends AnyRoute,
256
+ TParamVariant extends ParamVariant,
257
+ TFrom,
258
+ TTo,
259
+ TRoute extends AnyRoute = ResolveRoute<TRouteTree, TFrom, TTo>,
260
+ > = PostProcessParams<
261
+ TRoute['types'][TParamVariant extends 'PATH'
240
262
  ? 'allParams'
241
- : 'fullSearchSchemaInput',
242
- TFromParams = PostProcessParams<
243
- RouteByPath<TRouteTree, TFrom>['types'][TFromRouteType],
244
- TParamVariant
245
- >,
246
- TToParams = PostProcessParams<
247
- ResolveRoute<TRouteTree, TFrom, TTo>['types'][TToRouteType],
248
- TParamVariant
249
- >,
250
- TRelativeToParams = TParamVariant extends 'SEARCH'
263
+ : 'fullSearchSchemaInput'],
264
+ TParamVariant
265
+ >
266
+
267
+ type ResolveRelativeToParams<
268
+ TRouteTree extends AnyRoute,
269
+ TParamVariant extends ParamVariant,
270
+ TFrom,
271
+ TTo,
272
+ TToParams = ResolveToParams<TRouteTree, TParamVariant, TFrom, TTo>,
273
+ > = TParamVariant extends 'SEARCH'
274
+ ? TToParams
275
+ : string extends TFrom
251
276
  ? TToParams
252
- : true extends IsUnion<TFromParams>
253
- ? TToParams
254
- : MakeDifferenceOptional<TFromParams, TToParams>,
255
- TReducer = ParamsReducer<TFromParams, TRelativeToParams>,
256
- > = keyof PickRequired<TRelativeToParams> extends never
257
- ? Partial<MakeParamOption<TParamVariant, true | TReducer>>
258
- : TFromParams extends Expand<WithoutEmpty<PickRequired<TRelativeToParams>>>
259
- ? MakeParamOption<TParamVariant, true | TReducer>
260
- : MakeParamOption<TParamVariant, TReducer>
261
-
262
- type MakeParamOption<
277
+ : MakeDifferenceOptional<
278
+ ResolveFromParams<TRouteTree, TParamVariant, TFrom>,
279
+ TToParams
280
+ >
281
+
282
+ type MakeOptionalParams<
283
+ TRouteTree extends AnyRoute,
263
284
  TParamVariant extends ParamVariant,
264
- T,
265
- > = TParamVariant extends 'PATH'
266
- ? MakePathParamOptions<T>
267
- : MakeSearchParamOptions<T>
268
- type MakeSearchParamOptions<T> = { search: T }
269
- type MakePathParamOptions<T> = { params: T }
285
+ TFrom,
286
+ TTo,
287
+ > = TParamVariant extends 'SEARCH'
288
+ ? {
289
+ search?:
290
+ | true
291
+ | (ParamsReducer<
292
+ Expand<ResolveFromParams<TRouteTree, TParamVariant, TFrom>>,
293
+ Expand<
294
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
295
+ >
296
+ > & {})
297
+ }
298
+ : {
299
+ params?:
300
+ | true
301
+ | (ParamsReducer<
302
+ Expand<ResolveFromParams<TRouteTree, TParamVariant, TFrom>>,
303
+ Expand<
304
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
305
+ >
306
+ > & {})
307
+ }
308
+
309
+ type MakeRequiredParamsReducer<
310
+ TRouteTree extends AnyRoute,
311
+ TParamVariant extends ParamVariant,
312
+ TFrom,
313
+ TToParams,
314
+ TFromParams = ResolveFromParams<TRouteTree, TParamVariant, TFrom>,
315
+ > =
316
+ | ([TFromParams] extends [WithoutEmpty<PickRequired<TToParams>>]
317
+ ? true
318
+ : never)
319
+ | ParamsReducer<Expand<TFromParams>, TToParams>
320
+
321
+ export type MakeRequiredParams<
322
+ TRouteTree extends AnyRoute,
323
+ TParamVariant extends ParamVariant,
324
+ TFrom,
325
+ TTo,
326
+ > = TParamVariant extends 'SEARCH'
327
+ ? {
328
+ search: Expand<
329
+ MakeRequiredParamsReducer<
330
+ TRouteTree,
331
+ TParamVariant,
332
+ TFrom,
333
+ Expand<ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>>
334
+ >
335
+ > & {}
336
+ }
337
+ : {
338
+ params: Expand<
339
+ MakeRequiredParamsReducer<
340
+ TRouteTree,
341
+ TParamVariant,
342
+ TFrom,
343
+ Expand<ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>>
344
+ >
345
+ > & {}
346
+ }
347
+
348
+ export type IsRequiredParams<TParams> = keyof TParams extends infer K extends
349
+ keyof TParams
350
+ ? K extends any
351
+ ? undefined extends TParams[K]
352
+ ? never
353
+ : true
354
+ : never
355
+ : never
356
+
357
+ export type IsRequired<
358
+ TRouteTree extends AnyRoute,
359
+ TParamVariant extends ParamVariant,
360
+ TFrom,
361
+ TTo,
362
+ > = string extends TTo
363
+ ? string extends TFrom
364
+ ? never
365
+ : IsRequiredParams<
366
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
367
+ >
368
+ : IsRequiredParams<
369
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
370
+ >
371
+
372
+ export type ParamOptions<
373
+ TRouteTree extends AnyRoute,
374
+ TFrom,
375
+ TTo extends string,
376
+ TParamVariant extends ParamVariant,
377
+ > =
378
+ IsRequired<TRouteTree, TParamVariant, TFrom, TTo> extends never
379
+ ? MakeOptionalParams<TRouteTree, TParamVariant, TFrom, TTo>
380
+ : MakeRequiredParams<TRouteTree, TParamVariant, TFrom, TTo>
270
381
 
271
382
  export type SearchParamOptions<
272
383
  TRouteTree extends AnyRoute,
@@ -283,9 +394,9 @@ export type PathParamOptions<
283
394
  export type ToPathOption<
284
395
  TRouteTree extends AnyRoute = AnyRoute,
285
396
  TFrom extends RoutePaths<TRouteTree> | string = string,
286
- TTo extends string = '',
397
+ TTo extends string = string,
287
398
  > =
288
- | TTo
399
+ | CheckPath<TRouteTree, TTo, never, TFrom, TTo>
289
400
  | RelativeToPathAutoComplete<
290
401
  TRouteTree,
291
402
  NoInfer<TFrom> extends string ? NoInfer<TFrom> : '',
@@ -317,14 +428,8 @@ export type LinkOptions<
317
428
  disabled?: boolean
318
429
  }
319
430
 
320
- export type CheckPath<TRouteTree extends AnyRoute, TPass, TFrom, TTo> =
321
- ResolveRoute<TRouteTree, TFrom, TTo> extends never
322
- ? CheckPathError<TRouteTree>
323
- : TPass
324
-
325
- export type CheckPathError<TRouteTree extends AnyRoute> = {
326
- to: RoutePaths<TRouteTree>
327
- }
431
+ export type CheckPath<TRouteTree extends AnyRoute, TPass, TFail, TFrom, TTo> =
432
+ ResolveRoute<TRouteTree, TFrom, TTo> extends never ? TFail : TPass
328
433
 
329
434
  export type ResolveRelativePath<TFrom, TTo = '.'> = TFrom extends string
330
435
  ? TTo extends string
@@ -634,7 +739,7 @@ export type ActiveLinkOptions<
634
739
  export type LinkProps<
635
740
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
636
741
  TFrom extends RoutePaths<TRouteTree> | string = string,
637
- TTo extends string = '',
742
+ TTo extends string = string,
638
743
  TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
639
744
  TMaskTo extends string = '',
640
745
  > = ActiveLinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
@@ -644,6 +749,23 @@ export type LinkProps<
644
749
  | ((state: { isActive: boolean }) => React.ReactNode)
645
750
  }
646
751
 
752
+ type LinkComponentProps<TComp> = React.PropsWithoutRef<
753
+ TComp extends React.FC<infer TProps> | React.Component<infer TProps>
754
+ ? TProps
755
+ : TComp extends keyof JSX.IntrinsicElements
756
+ ? Omit<React.HTMLProps<TComp>, 'children' | 'preload'>
757
+ : never
758
+ > &
759
+ React.RefAttributes<
760
+ TComp extends
761
+ | React.FC<{ ref: infer TRef }>
762
+ | React.Component<{ ref: infer TRef }>
763
+ ? TRef
764
+ : TComp extends keyof JSX.IntrinsicElements
765
+ ? React.ComponentRef<TComp>
766
+ : never
767
+ >
768
+
647
769
  export type LinkComponent<TComp> = <
648
770
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
649
771
  TFrom extends RoutePaths<TRouteTree> | string = string,
@@ -651,23 +773,8 @@ export type LinkComponent<TComp> = <
651
773
  TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
652
774
  TMaskTo extends string = '',
653
775
  >(
654
- props: React.PropsWithoutRef<
655
- LinkProps<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
656
- (TComp extends React.FC<infer TProps> | React.Component<infer TProps>
657
- ? TProps
658
- : TComp extends keyof JSX.IntrinsicElements
659
- ? Omit<React.HTMLProps<TComp>, 'children' | 'preload'>
660
- : never)
661
- > &
662
- React.RefAttributes<
663
- TComp extends
664
- | React.FC<{ ref: infer TRef }>
665
- | React.Component<{ ref: infer TRef }>
666
- ? TRef
667
- : TComp extends keyof JSX.IntrinsicElements
668
- ? React.ComponentRef<TComp>
669
- : never
670
- >,
776
+ props: LinkProps<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
777
+ LinkComponentProps<TComp>,
671
778
  ) => React.ReactElement
672
779
 
673
780
  export function createLink<const TComp>(Comp: TComp): LinkComponent<TComp> {
package/src/redirects.ts CHANGED
@@ -31,8 +31,10 @@ export type ResolvedRedirect<
31
31
  TMaskTo extends string = '',
32
32
  > = PickAsRequired<
33
33
  Redirect<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
34
- 'code' | 'statusCode' | 'href' | 'headers'
35
- >
34
+ 'code' | 'statusCode' | 'headers'
35
+ > & {
36
+ href: string
37
+ }
36
38
 
37
39
  export function redirect<
38
40
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],