@tanstack/react-router 1.39.7 → 1.39.8

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.
@@ -45,19 +45,14 @@ export type NavigateFn = <
45
45
  },
46
46
  ) => Promise<void>
47
47
 
48
- export type BuildLocationFn<TRouteTree extends AnyRoute> = <
48
+ export type BuildLocationFn = <
49
49
  TTo extends string,
50
- TFrom extends RoutePaths<TRouteTree> | string = string,
51
- TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
50
+ TRouter extends AnyRouter = RegisteredRouter,
51
+ TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
52
+ TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,
52
53
  TMaskTo extends string = '',
53
54
  >(
54
- opts: ToOptions<
55
- Router<TRouteTree, 'never'>,
56
- TFrom,
57
- TTo,
58
- TMaskFrom,
59
- TMaskTo
60
- > & {
55
+ opts: ToOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
61
56
  leaveParams?: boolean
62
57
  },
63
58
  ) => ParsedLocation
package/src/fileRoute.ts CHANGED
@@ -13,6 +13,7 @@ import type {
13
13
  AnyRoute,
14
14
  AnySearchSchema,
15
15
  FileBaseRouteOptions,
16
+ InferAllContext,
16
17
  ResolveAllContext,
17
18
  ResolveAllParamsFromParent,
18
19
  ResolveFullSearchSchema,
@@ -92,14 +93,12 @@ export class FileRoute<
92
93
  TRouteContextReturn = RouteContext,
93
94
  TRouteContext = ResolveRouteContext<TRouteContextReturn>,
94
95
  TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
95
- TRouterContext = AnyContext,
96
96
  TLoaderDeps extends Record<string, any> = {},
97
97
  TLoaderDataReturn = {},
98
98
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
99
99
  TChildren = unknown,
100
100
  >(
101
101
  options?: FileBaseRouteOptions<
102
- TParentRoute,
103
102
  TPath,
104
103
  TSearchSchemaInput,
105
104
  TSearchSchema,
@@ -107,8 +106,7 @@ export class FileRoute<
107
106
  TParams,
108
107
  TAllParams,
109
108
  TRouteContextReturn,
110
- TRouteContext,
111
- TRouterContext,
109
+ InferAllContext<TParentRoute>,
112
110
  TAllContext,
113
111
  TLoaderDeps,
114
112
  TLoaderDataReturn
@@ -138,7 +136,6 @@ export class FileRoute<
138
136
  TRouteContextReturn,
139
137
  TRouteContext,
140
138
  TAllContext,
141
- TRouterContext,
142
139
  TLoaderDeps,
143
140
  TLoaderDataReturn,
144
141
  TLoaderData,
@@ -169,14 +166,12 @@ export function FileRouteLoader<
169
166
  TRoute['types']['allParams'],
170
167
  TRoute['types']['loaderDeps'],
171
168
  TRoute['types']['allContext'],
172
- TRoute['types']['routeContext'],
173
169
  TLoaderData
174
170
  >,
175
171
  ) => RouteLoaderFn<
176
172
  TRoute['types']['allParams'],
177
173
  TRoute['types']['loaderDeps'],
178
174
  TRoute['types']['allContext'],
179
- TRoute['types']['routeContext'],
180
175
  NoInfer<TLoaderData>
181
176
  > {
182
177
  warning(
package/src/route.ts CHANGED
@@ -61,7 +61,7 @@ export type RouteOptions<
61
61
  TAllParams = TParams,
62
62
  TRouteContextReturn = RouteContext,
63
63
  TRouteContext = RouteContext,
64
- TRouterContext = AnyContext,
64
+ TParentAllContext = AnyContext,
65
65
  TAllContext = AnyContext,
66
66
  TLoaderDeps extends Record<string, any> = {},
67
67
  TLoaderDataReturn = {},
@@ -76,8 +76,7 @@ export type RouteOptions<
76
76
  TParams,
77
77
  TAllParams,
78
78
  TRouteContextReturn,
79
- TRouteContext,
80
- TRouterContext,
79
+ TParentAllContext,
81
80
  TAllContext,
82
81
  TLoaderDeps,
83
82
  TLoaderDataReturn
@@ -98,7 +97,6 @@ export type ParamsFallback<
98
97
  > = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams
99
98
 
100
99
  export type FileBaseRouteOptions<
101
- TParentRoute extends AnyRoute = AnyRoute,
102
100
  TPath extends string = string,
103
101
  TSearchSchemaInput = Record<string, unknown>,
104
102
  TSearchSchema = {},
@@ -106,8 +104,7 @@ export type FileBaseRouteOptions<
106
104
  TParams = {},
107
105
  TAllParams = ParamsFallback<TPath, TParams>,
108
106
  TRouteContextReturn = RouteContext,
109
- TRouteContext = RouteContext,
110
- TRouterContext = AnyContext,
107
+ TParentAllContext = AnyContext,
111
108
  TAllContext = AnyContext,
112
109
  TLoaderDeps extends Record<string, any> = {},
113
110
  TLoaderDataReturn = {},
@@ -116,12 +113,7 @@ export type FileBaseRouteOptions<
116
113
  shouldReload?:
117
114
  | boolean
118
115
  | ((
119
- match: LoaderFnContext<
120
- TAllParams,
121
- TFullSearchSchema,
122
- TAllContext,
123
- TRouteContext
124
- >,
116
+ match: LoaderFnContext<TAllParams, TFullSearchSchema, TAllContext>,
125
117
  ) => any)
126
118
  // This async function is called before a route is loaded.
127
119
  // If an error is thrown here, the route's loader will not be called.
@@ -129,17 +121,15 @@ export type FileBaseRouteOptions<
129
121
  // If thrown during a preload event, the error will be logged to the console.
130
122
  beforeLoad?: BeforeLoadFn<
131
123
  TFullSearchSchema,
132
- TParentRoute,
133
124
  TAllParams,
134
125
  TRouteContextReturn,
135
- TRouterContext
126
+ TParentAllContext
136
127
  >
137
128
  loaderDeps?: (opts: { search: TFullSearchSchema }) => TLoaderDeps
138
129
  loader?: RouteLoaderFn<
139
130
  TAllParams,
140
131
  NoInfer<TLoaderDeps>,
141
132
  NoInfer<TAllContext>,
142
- NoInfer<TRouteContext>,
143
133
  TLoaderDataReturn
144
134
  >
145
135
  } & (
@@ -170,14 +160,12 @@ export type BaseRouteOptions<
170
160
  TParams = {},
171
161
  TAllParams = ParamsFallback<TPath, TParams>,
172
162
  TRouteContextReturn = RouteContext,
173
- TRouteContext = RouteContext,
174
- TRouterContext = AnyContext,
163
+ TParentAllContext = AnyContext,
175
164
  TAllContext = AnyContext,
176
165
  TLoaderDeps extends Record<string, any> = {},
177
166
  TLoaderDataReturn = {},
178
167
  > = RoutePathOptions<TCustomId, TPath> &
179
168
  FileBaseRouteOptions<
180
- TParentRoute,
181
169
  TPath,
182
170
  TSearchSchemaInput,
183
171
  TSearchSchema,
@@ -185,8 +173,7 @@ export type BaseRouteOptions<
185
173
  TParams,
186
174
  TAllParams,
187
175
  TRouteContextReturn,
188
- TRouteContext,
189
- TRouterContext,
176
+ TParentAllContext,
190
177
  TAllContext,
191
178
  TLoaderDeps,
192
179
  TLoaderDataReturn
@@ -196,23 +183,21 @@ export type BaseRouteOptions<
196
183
 
197
184
  type BeforeLoadFn<
198
185
  in out TFullSearchSchema,
199
- in out TParentRoute extends AnyRoute,
200
186
  in out TAllParams,
201
187
  TRouteContextReturn,
202
- in out TRouterContext = AnyContext,
203
- in out TContext = IsAny<TParentRoute['types']['allContext'], TRouterContext>,
188
+ in out TParentAllContext,
204
189
  > = (opts: {
205
190
  search: TFullSearchSchema
206
191
  abortController: AbortController
207
192
  preload: boolean
208
193
  params: TAllParams
209
- context: TContext
194
+ context: TParentAllContext
210
195
  location: ParsedLocation
211
196
  /**
212
197
  * @deprecated Use `throw redirect({ to: '/somewhere' })` instead
213
198
  **/
214
199
  navigate: NavigateFn
215
- buildLocation: BuildLocationFn<TParentRoute>
200
+ buildLocation: BuildLocationFn
216
201
  cause: 'preload' | 'enter' | 'stay'
217
202
  }) => Promise<TRouteContextReturn> | TRouteContextReturn | void
218
203
 
@@ -333,23 +318,21 @@ export type RouteLoaderFn<
333
318
  in out TAllParams = {},
334
319
  in out TLoaderDeps extends Record<string, any> = {},
335
320
  in out TAllContext = AnyContext,
336
- in out TRouteContext = AnyContext,
337
321
  TLoaderData = undefined,
338
322
  > = (
339
- match: LoaderFnContext<TAllParams, TLoaderDeps, TAllContext, TRouteContext>,
323
+ match: LoaderFnContext<TAllParams, TLoaderDeps, TAllContext>,
340
324
  ) => Promise<TLoaderData> | TLoaderData
341
325
 
342
326
  export interface LoaderFnContext<
343
327
  in out TAllParams = {},
344
328
  in out TLoaderDeps = {},
345
329
  in out TAllContext = AnyContext,
346
- in out TRouteContext = AnyContext,
347
330
  > {
348
331
  abortController: AbortController
349
332
  preload: boolean
350
333
  params: TAllParams
351
334
  deps: TLoaderDeps
352
- context: Assign<TAllContext, TRouteContext>
335
+ context: TAllContext
353
336
  location: ParsedLocation // Do not supply search schema here so as to demotivate people from trying to shortcut loaderDeps
354
337
  /**
355
338
  * @deprecated Use `throw redirect({ to: '/somewhere' })` instead
@@ -386,6 +369,22 @@ export type InferFullSearchSchemaInput<TRoute> = TRoute extends {
386
369
  ? TFullSearchSchemaInput
387
370
  : {}
388
371
 
372
+ export type InferAllParams<TRoute> = TRoute extends {
373
+ types: {
374
+ allParams: infer TAllParams
375
+ }
376
+ }
377
+ ? TAllParams
378
+ : {}
379
+
380
+ export type InferAllContext<TRoute> = TRoute extends {
381
+ types: {
382
+ allContext: infer TAllContext
383
+ }
384
+ }
385
+ ? TAllContext
386
+ : {}
387
+
389
388
  export type ResolveSearchSchemaUsed<TSearchSchemaInput, TSearchSchema> =
390
389
  TSearchSchemaInput extends SearchSchemaInput
391
390
  ? Omit<TSearchSchemaInput, keyof SearchSchemaInput>
@@ -394,16 +393,12 @@ export type ResolveSearchSchemaUsed<TSearchSchemaInput, TSearchSchema> =
394
393
  export type ResolveFullSearchSchema<
395
394
  TParentRoute extends AnyRoute,
396
395
  TSearchSchema,
397
- > = unknown extends TParentRoute
398
- ? TSearchSchema
399
- : Assign<TParentRoute['types']['fullSearchSchema'], TSearchSchema>
396
+ > = Assign<InferFullSearchSchema<TParentRoute>, TSearchSchema>
400
397
 
401
398
  export type ResolveFullSearchSchemaInput<
402
399
  TParentRoute extends AnyRoute,
403
400
  TSearchSchemaUsed,
404
- > = unknown extends TParentRoute
405
- ? TSearchSchemaUsed
406
- : Assign<TParentRoute['types']['fullSearchSchemaInput'], TSearchSchemaUsed>
401
+ > = Assign<InferFullSearchSchemaInput<TParentRoute>, TSearchSchemaUsed>
407
402
 
408
403
  export type ResolveRouteContext<TRouteContextReturn> = [
409
404
  TRouteContextReturn,
@@ -414,7 +409,7 @@ export type ResolveRouteContext<TRouteContextReturn> = [
414
409
  export type ResolveAllContext<
415
410
  TParentRoute extends AnyRoute,
416
411
  TRouteContext,
417
- > = Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
412
+ > = Assign<InferAllContext<TParentRoute>, TRouteContext>
418
413
 
419
414
  export type ResolveLoaderData<TLoaderDataReturn> = [TLoaderDataReturn] extends [
420
415
  never,
@@ -442,16 +437,13 @@ export interface AnyRoute
442
437
  any,
443
438
  any,
444
439
  any,
445
- any,
446
440
  any
447
441
  > {}
448
442
 
449
443
  export type ResolveAllParamsFromParent<
450
444
  TParentRoute extends AnyRoute,
451
445
  TParams,
452
- > = unknown extends TParentRoute
453
- ? TParams
454
- : Assign<TParentRoute['types']['allParams'], TParams>
446
+ > = Assign<InferAllParams<TParentRoute>, TParams>
455
447
 
456
448
  export type RouteConstraints = {
457
449
  TParentRoute: AnyRoute
@@ -595,11 +587,7 @@ export class Route<
595
587
  in out TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
596
588
  TRouteContextReturn = RouteContext,
597
589
  in out TRouteContext = ResolveRouteContext<TRouteContextReturn>,
598
- in out TAllContext = Assign<
599
- IsAny<TParentRoute['types']['allContext'], {}>,
600
- TRouteContext
601
- >,
602
- in out TRouterContext = AnyContext,
590
+ in out TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
603
591
  in out TLoaderDeps extends Record<string, any> = {},
604
592
  TLoaderDataReturn = {},
605
593
  in out TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -617,7 +605,7 @@ export class Route<
617
605
  TAllParams,
618
606
  TRouteContextReturn,
619
607
  TRouteContext,
620
- TRouterContext,
608
+ InferAllContext<TParentRoute>,
621
609
  TAllContext,
622
610
  TLoaderDeps,
623
611
  TLoaderDataReturn,
@@ -654,7 +642,7 @@ export class Route<
654
642
  TAllParams,
655
643
  TRouteContextReturn,
656
644
  TRouteContext,
657
- TRouterContext,
645
+ InferAllContext<TParentRoute>,
658
646
  TAllContext,
659
647
  TLoaderDeps,
660
648
  TLoaderDataReturn,
@@ -688,7 +676,6 @@ export class Route<
688
676
  routeContext: TRouteContext
689
677
  allContext: TAllContext
690
678
  children: TChildren
691
- routerContext: TRouterContext
692
679
  loaderData: TLoaderData
693
680
  loaderDeps: TLoaderDeps
694
681
  }
@@ -708,7 +695,7 @@ export class Route<
708
695
  TAllParams,
709
696
  TRouteContextReturn,
710
697
  TRouteContext,
711
- TRouterContext,
698
+ InferAllContext<TParentRoute>,
712
699
  TAllContext,
713
700
  TLoaderDeps,
714
701
  TLoaderDataReturn,
@@ -788,7 +775,6 @@ export class Route<
788
775
  TRouteContextReturn,
789
776
  TRouteContext,
790
777
  TAllContext,
791
- TRouterContext,
792
778
  TLoaderDeps,
793
779
  TLoaderDataReturn,
794
780
  TLoaderData,
@@ -801,13 +787,7 @@ export class Route<
801
787
  }
802
788
 
803
789
  updateLoader = <TNewLoaderData = unknown>(options: {
804
- loader: RouteLoaderFn<
805
- TAllParams,
806
- TLoaderDeps,
807
- TAllContext,
808
- TRouteContext,
809
- TNewLoaderData
810
- >
790
+ loader: RouteLoaderFn<TAllParams, TLoaderDeps, TAllContext, TNewLoaderData>
811
791
  }) => {
812
792
  Object.assign(this.options, options)
813
793
  return this as unknown as Route<
@@ -826,7 +806,6 @@ export class Route<
826
806
  TRouteContextReturn,
827
807
  TRouteContext,
828
808
  TAllContext,
829
- TRouterContext,
830
809
  TLoaderDeps,
831
810
  TNewLoaderData,
832
811
  TChildren
@@ -931,11 +910,7 @@ export function createRoute<
931
910
  TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
932
911
  TRouteContextReturn = RouteContext,
933
912
  TRouteContext = ResolveRouteContext<TRouteContextReturn>,
934
- TAllContext = Assign<
935
- IsAny<TParentRoute['types']['allContext'], {}>,
936
- TRouteContext
937
- >,
938
- TRouterContext = AnyContext,
913
+ TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
939
914
  TLoaderDeps extends Record<string, any> = {},
940
915
  TLoaderDataReturn = {},
941
916
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -952,7 +927,7 @@ export function createRoute<
952
927
  TAllParams,
953
928
  TRouteContextReturn,
954
929
  TRouteContext,
955
- TRouterContext,
930
+ InferAllContext<TParentRoute>,
956
931
  TAllContext,
957
932
  TLoaderDeps,
958
933
  TLoaderDataReturn,
@@ -975,7 +950,6 @@ export function createRoute<
975
950
  TRouteContextReturn,
976
951
  TRouteContext,
977
952
  TAllContext,
978
- TRouterContext,
979
953
  TLoaderDeps,
980
954
  TLoaderDataReturn,
981
955
  TLoaderData,
@@ -1006,7 +980,7 @@ export type RootRouteOptions<
1006
980
  {}, // TAllParams
1007
981
  TRouteContextReturn, // TRouteContextReturn
1008
982
  TRouteContext, // TRouteContext
1009
- TRouterContext,
983
+ TRouterContext, // TParentAllContext
1010
984
  Assign<TRouterContext, TRouteContext>, // TAllContext
1011
985
  TLoaderDeps,
1012
986
  TLoaderDataReturn, // TLoaderDataReturn,
@@ -1090,7 +1064,6 @@ export class RootRoute<
1090
1064
  TRouteContextReturn, // TRouteContextReturn
1091
1065
  TRouteContext, // TRouteContext
1092
1066
  Assign<TRouterContext, TRouteContext>, // TAllContext
1093
- TRouterContext, // TRouterContext
1094
1067
  TLoaderDeps,
1095
1068
  TLoaderDataReturn,
1096
1069
  TLoaderData,
@@ -1277,7 +1250,6 @@ export class NotFoundRoute<
1277
1250
  TRouteContextReturn = AnyContext,
1278
1251
  TRouteContext = RouteContext,
1279
1252
  TAllContext = ResolveAllContext<TParentRoute, TRouteContext>,
1280
- TRouterContext = AnyContext,
1281
1253
  TLoaderDeps extends Record<string, any> = {},
1282
1254
  TLoaderDataReturn = {},
1283
1255
  TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
@@ -1298,7 +1270,6 @@ export class NotFoundRoute<
1298
1270
  TRouteContextReturn,
1299
1271
  TRouteContext,
1300
1272
  TAllContext,
1301
- TRouterContext,
1302
1273
  TLoaderDeps,
1303
1274
  TLoaderDataReturn,
1304
1275
  TLoaderData,
@@ -1317,7 +1288,7 @@ export class NotFoundRoute<
1317
1288
  {},
1318
1289
  TRouteContextReturn,
1319
1290
  TRouteContext,
1320
- TRouterContext,
1291
+ InferAllContext<TParentRoute>,
1321
1292
  TAllContext,
1322
1293
  TLoaderDeps,
1323
1294
  TLoaderDataReturn,
package/src/router.ts CHANGED
@@ -43,6 +43,7 @@ import type {
43
43
  ErrorRouteComponent,
44
44
  LoaderFnContext,
45
45
  NotFoundRouteComponent,
46
+ RootRoute,
46
47
  RouteMask,
47
48
  } from './route'
48
49
  import type {
@@ -109,13 +110,28 @@ export type HydrationCtx = {
109
110
  payload: Record<string, any>
110
111
  }
111
112
 
113
+ export type InferRouterContext<TRouteTree extends AnyRoute> =
114
+ TRouteTree extends RootRoute<
115
+ any,
116
+ any,
117
+ any,
118
+ any,
119
+ any,
120
+ infer TRouterContext extends AnyContext,
121
+ any,
122
+ any,
123
+ any
124
+ >
125
+ ? TRouterContext
126
+ : AnyContext
127
+
112
128
  export type RouterContextOptions<TRouteTree extends AnyRoute> =
113
- AnyContext extends TRouteTree['types']['routerContext']
129
+ AnyContext extends InferRouterContext<TRouteTree>
114
130
  ? {
115
- context?: TRouteTree['types']['routerContext']
131
+ context?: InferRouterContext<TRouteTree>
116
132
  }
117
133
  : {
118
- context: TRouteTree['types']['routerContext']
134
+ context: InferRouterContext<TRouteTree>
119
135
  }
120
136
 
121
137
  export type TrailingSlashOption = 'always' | 'never' | 'preserve'
@@ -261,7 +277,7 @@ export interface RouterOptions<
261
277
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#context-property)
262
278
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/router-context)
263
279
  */
264
- context?: TRouteTree['types']['routerContext']
280
+ context?: InferRouterContext<TRouteTree>
265
281
  /**
266
282
  * A function that will be called when the router is dehydrated.
267
283
  * The return value of this function will be serialized and stored in the router's dehydrated state.
@@ -1094,7 +1110,7 @@ export class Router<
1094
1110
  })
1095
1111
  }
1096
1112
 
1097
- buildLocation: BuildLocationFn<TRouteTree> = (opts) => {
1113
+ buildLocation: BuildLocationFn = (opts) => {
1098
1114
  const build = (
1099
1115
  dest: BuildNextOptions & {
1100
1116
  unmaskOnReload?: boolean