@tanstack/react-router 1.27.0 → 1.28.1

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.
package/src/fileRoute.ts CHANGED
@@ -24,7 +24,7 @@ import type {
24
24
  TrimPathLeft,
25
25
  UpdatableRouteOptions,
26
26
  } from './route'
27
- import type { Assign, Expand, IsAny } from './utils'
27
+ import type { Assign, IsAny } from './utils'
28
28
  import type { RouteMatch } from './Matches'
29
29
  import type { NoInfer } from '@tanstack/react-store'
30
30
  import type { RegisteredRouter } from './router'
@@ -67,7 +67,7 @@ export type RemoveUnderScores<T extends string> = Replace<
67
67
  >
68
68
 
69
69
  type RemoveRouteGroups<T extends string> =
70
- T extends `${infer Before}(${infer RouteGroup})${infer After}`
70
+ T extends `${infer Before}(${string})${infer After}`
71
71
  ? RemoveRouteGroups<`${Before}${After}`>
72
72
  : T
73
73
 
@@ -76,32 +76,22 @@ type NormalizeSlashes<T extends string> =
76
76
  ? NormalizeSlashes<`${Before}/${After}`>
77
77
  : T
78
78
 
79
- type ReplaceFirstOccurrence<
80
- TValue extends string,
81
- TSearch extends string,
82
- TReplacement extends string,
83
- > = TValue extends `${infer Prefix}${TSearch}${infer Suffix}`
84
- ? `${Prefix}${TReplacement}${Suffix}`
85
- : TValue
86
-
87
79
  export type ResolveFilePath<
88
80
  TParentRoute extends AnyRoute,
89
81
  TFilePath extends string,
90
82
  > = TParentRoute['id'] extends RootRouteId
91
83
  ? TrimPathLeft<TFilePath>
92
- : ReplaceFirstOccurrence<
93
- TrimPathLeft<TFilePath>,
94
- TrimPathLeft<TParentRoute['types']['customId']>,
95
- ''
96
- >
84
+ : TFilePath extends `${TParentRoute['types']['customId']}${infer TRest}`
85
+ ? TRest
86
+ : TFilePath
97
87
 
98
88
  export type FileRoutePath<
99
89
  TParentRoute extends AnyRoute,
100
90
  TFilePath extends string,
101
91
  TResolvedFilePath = ResolveFilePath<TParentRoute, TFilePath>,
102
- > = TResolvedFilePath extends `_${infer _}`
92
+ > = TResolvedFilePath extends `_${string}`
103
93
  ? ''
104
- : TResolvedFilePath extends `/_${infer _}`
94
+ : TResolvedFilePath extends `/_${string}`
105
95
  ? ''
106
96
  : TResolvedFilePath
107
97
 
@@ -154,34 +144,27 @@ export class FileRoute<
154
144
  createRoute = <
155
145
  TSearchSchemaInput extends RouteConstraints['TSearchSchema'] = {},
156
146
  TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
157
- TSearchSchemaUsed extends Record<
158
- string,
159
- any
160
- > = TSearchSchemaInput extends SearchSchemaInput
147
+ TSearchSchemaUsed = TSearchSchemaInput extends SearchSchemaInput
161
148
  ? Omit<TSearchSchemaInput, keyof SearchSchemaInput>
162
149
  : TSearchSchema,
163
- TFullSearchSchemaInput extends
164
- RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchemaInput<
150
+ TFullSearchSchemaInput = ResolveFullSearchSchemaInput<
165
151
  TParentRoute,
166
152
  TSearchSchemaUsed
167
153
  >,
168
154
  TFullSearchSchema = ResolveFullSearchSchema<TParentRoute, TSearchSchema>,
169
- TParams extends RouteConstraints['TParams'] = Expand<
170
- Record<ParsePathParams<TPath>, string>
171
- >,
172
- TAllParams extends RouteConstraints['TAllParams'] = MergeFromFromParent<
155
+ TParams = Record<ParsePathParams<TPath>, string>,
156
+ TAllParams = MergeFromFromParent<
173
157
  TParentRoute['types']['allParams'],
174
158
  TParams
175
159
  >,
176
160
  TRouteContextReturn extends
177
161
  RouteConstraints['TRouteContext'] = RouteContext,
178
- TRouteContext extends RouteConstraints['TRouteContext'] = [
179
- TRouteContextReturn,
180
- ] extends [never]
162
+ TRouteContext = [TRouteContextReturn] extends [never]
181
163
  ? RouteContext
182
164
  : TRouteContextReturn,
183
- TAllContext = Expand<
184
- Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
165
+ TAllContext = Assign<
166
+ IsAny<TParentRoute['types']['allContext'], {}>,
167
+ TRouteContext
185
168
  >,
186
169
  TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
187
170
  TLoaderDeps extends Record<string, any> = {},
@@ -190,7 +173,6 @@ export class FileRoute<
190
173
  ? undefined
191
174
  : TLoaderDataReturn,
192
175
  TChildren extends RouteConstraints['TChildren'] = unknown,
193
- TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
194
176
  >(
195
177
  options?: FileBaseRouteOptions<
196
178
  TParentRoute,
@@ -228,8 +210,7 @@ export class FileRoute<
228
210
  TLoaderDeps,
229
211
  TLoaderDataReturn,
230
212
  TLoaderData,
231
- TChildren,
232
- TRouteTree
213
+ TChildren
233
214
  > => {
234
215
  warning(
235
216
  this.silent,
@@ -294,7 +275,7 @@ export class LazyRoute<TRoute extends AnyRoute> {
294
275
 
295
276
  useMatch = <
296
277
  TRouteMatchState = RouteMatch<
297
- TRoute['types']['routeTree'],
278
+ RegisteredRouter['routeTree'],
298
279
  TRoute['types']['id']
299
280
  >,
300
281
  TSelected = TRouteMatchState,
package/src/index.tsx CHANGED
@@ -67,7 +67,6 @@ export {
67
67
  type ActiveOptions,
68
68
  type LinkOptions,
69
69
  type CheckPath,
70
- type CheckPathError,
71
70
  type ResolveRelativePath,
72
71
  type UseLinkPropsOptions,
73
72
  type ActiveLinkOptions,
package/src/link.tsx CHANGED
@@ -84,67 +84,78 @@ export type RemoveLeadingSlashes<T> = T extends `/${infer R}`
84
84
  ? RemoveLeadingSlashes<R>
85
85
  : T
86
86
 
87
+ export type ResolvePaths<TRouteTree extends AnyRoute, TSearchPath> =
88
+ RouteByPath<TRouteTree, RemoveTrailingSlashes<TSearchPath>> extends never
89
+ ? RoutePaths<TRouteTree>
90
+ : RoutePaths<RouteByPath<TRouteTree, RemoveTrailingSlashes<TSearchPath>>>
91
+
87
92
  export type SearchPaths<
88
- TPaths,
93
+ TRouteTree extends AnyRoute,
89
94
  TSearchPath extends string,
95
+ TPaths = ResolvePaths<TRouteTree, TSearchPath>,
90
96
  > = TPaths extends `${RemoveTrailingSlashes<TSearchPath>}/${infer TRest}`
91
97
  ? TRest
92
98
  : never
93
99
 
94
100
  export type SearchRelativePathAutoComplete<
101
+ TRouteTree extends AnyRoute,
95
102
  TTo extends string,
96
103
  TSearchPath extends string,
97
- TPaths,
98
- TSearchedPaths = SearchPaths<TPaths, TSearchPath>,
99
- > = TSearchedPaths extends string ? `${TTo}/${TSearchedPaths}` : never
104
+ > = `${TTo}/${SearchPaths<TRouteTree, TSearchPath>}`
100
105
 
101
106
  export type RelativeToParentPathAutoComplete<
107
+ TRouteTree extends AnyRoute,
102
108
  TFrom extends string,
103
109
  TTo extends string,
104
- TPaths,
105
110
  TResolvedPath extends string = RemoveTrailingSlashes<
106
111
  ResolveRelativePath<TFrom, TTo>
107
112
  >,
108
113
  > =
109
- | SearchRelativePathAutoComplete<TTo, TResolvedPath, TPaths>
114
+ | SearchRelativePathAutoComplete<TRouteTree, TTo, TResolvedPath>
110
115
  | (TResolvedPath extends '' ? never : `${TTo}/../`)
111
116
 
112
117
  export type RelativeToCurrentPathAutoComplete<
118
+ TRouteTree extends AnyRoute,
113
119
  TFrom extends string,
114
120
  TTo extends string,
115
121
  TRestTo extends string,
116
- TPaths,
117
122
  TResolvedPath extends
118
123
  string = RemoveTrailingSlashes<`${RemoveTrailingSlashes<TFrom>}/${RemoveLeadingSlashes<TRestTo>}`>,
119
- > = SearchRelativePathAutoComplete<TTo, TResolvedPath, TPaths>
124
+ > = SearchRelativePathAutoComplete<TRouteTree, TTo, TResolvedPath>
120
125
 
121
- export type AbsolutePathAutoComplete<TFrom extends string, TPaths> =
126
+ export type AbsolutePathAutoComplete<
127
+ TRouteTree extends AnyRoute,
128
+ TFrom extends string,
129
+ > =
122
130
  | (string extends TFrom
123
131
  ? './'
124
132
  : TFrom extends `/`
125
133
  ? never
126
- : SearchPaths<TPaths, TFrom> extends ''
134
+ : SearchPaths<TRouteTree, TFrom> extends ''
127
135
  ? never
128
136
  : './')
129
137
  | (string extends TFrom ? '../' : TFrom extends `/` ? never : '../')
130
- | TPaths
131
- | (TFrom extends '/' ? never : SearchPaths<TPaths, TFrom>)
138
+ | RoutePaths<TRouteTree>
139
+ | (TFrom extends '/' ? never : SearchPaths<TRouteTree, TFrom>)
132
140
 
133
141
  export type RelativeToPathAutoComplete<
134
142
  TRouteTree extends AnyRoute,
135
143
  TFrom extends string,
136
144
  TTo extends string,
137
- TPaths = RoutePaths<TRouteTree>,
138
145
  > = TTo extends `..${string}`
139
- ? RelativeToParentPathAutoComplete<TFrom, RemoveTrailingSlashes<TTo>, TPaths>
146
+ ? RelativeToParentPathAutoComplete<
147
+ TRouteTree,
148
+ TFrom,
149
+ RemoveTrailingSlashes<TTo>
150
+ >
140
151
  : TTo extends `./${infer TRestTTo}`
141
152
  ? RelativeToCurrentPathAutoComplete<
153
+ TRouteTree,
142
154
  TFrom,
143
155
  RemoveTrailingSlashes<TTo>,
144
- TRestTTo,
145
- TPaths
156
+ TRestTTo
146
157
  >
147
- : AbsolutePathAutoComplete<TFrom, TPaths>
158
+ : AbsolutePathAutoComplete<TRouteTree, TFrom>
148
159
 
149
160
  export type NavigateOptions<
150
161
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
@@ -183,19 +194,19 @@ export type ToSubOptions<
183
194
  TFrom extends RoutePaths<TRouteTree> | string = string,
184
195
  TTo extends string = '',
185
196
  > = {
186
- to?: ToPathOption<TRouteTree, TFrom, TTo>
197
+ to?: ToPathOption<TRouteTree, TFrom, TTo> & {}
187
198
  hash?: true | Updater<string>
188
199
  state?: true | NonNullableUpdater<HistoryState>
189
200
  // 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>
201
+ from?: RoutePathsAutoComplete<TRouteTree, TFrom> & {}
191
202
  // // 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> &
203
+ } & SearchParamOptions<TRouteTree, TFrom, TTo> &
194
204
  PathParamOptions<TRouteTree, TFrom, TTo>
195
205
 
196
206
  type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
197
207
 
198
208
  type ParamVariant = 'PATH' | 'SEARCH'
209
+
199
210
  type ExcludeRootSearchSchema<T, TExcluded = Exclude<T, RootSearchSchema>> = [
200
211
  TExcluded,
201
212
  ] extends [never]
@@ -224,49 +235,147 @@ type PostProcessParams<
224
235
  TParamVariant extends ParamVariant,
225
236
  > = TParamVariant extends 'SEARCH' ? ExcludeRootSearchSchema<T> : T
226
237
 
227
- export type ParamOptions<
238
+ type ResolveFromParams<
228
239
  TRouteTree extends AnyRoute,
229
- TFrom,
230
- TTo extends string,
231
240
  TParamVariant extends ParamVariant,
241
+ TFrom,
232
242
  TFromRouteType extends
233
243
  | 'allParams'
234
244
  | 'fullSearchSchema' = TParamVariant extends 'PATH'
235
245
  ? 'allParams'
236
246
  : 'fullSearchSchema',
237
- TToRouteType extends
238
- | 'allParams'
239
- | 'fullSearchSchemaInput' = TParamVariant extends 'PATH'
247
+ > = PostProcessParams<
248
+ RouteByPath<TRouteTree, TFrom>['types'][TFromRouteType],
249
+ TParamVariant
250
+ >
251
+
252
+ type ResolveToParams<
253
+ TRouteTree extends AnyRoute,
254
+ TParamVariant extends ParamVariant,
255
+ TFrom,
256
+ TTo,
257
+ TRoute extends AnyRoute = ResolveRoute<TRouteTree, TFrom, TTo>,
258
+ > = PostProcessParams<
259
+ TRoute['types'][TParamVariant extends 'PATH'
240
260
  ? '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'
261
+ : 'fullSearchSchemaInput'],
262
+ TParamVariant
263
+ >
264
+
265
+ type ResolveRelativeToParams<
266
+ TRouteTree extends AnyRoute,
267
+ TParamVariant extends ParamVariant,
268
+ TFrom,
269
+ TTo,
270
+ TToParams = ResolveToParams<TRouteTree, TParamVariant, TFrom, TTo>,
271
+ > = TParamVariant extends 'SEARCH'
272
+ ? TToParams
273
+ : string extends TFrom
251
274
  ? 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<
275
+ : MakeDifferenceOptional<
276
+ ResolveFromParams<TRouteTree, TParamVariant, TFrom>,
277
+ TToParams
278
+ >
279
+
280
+ type MakeOptionalParams<
281
+ TRouteTree extends AnyRoute,
263
282
  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 }
283
+ TFrom,
284
+ TTo,
285
+ > = TParamVariant extends 'SEARCH'
286
+ ? {
287
+ search?:
288
+ | true
289
+ | (ParamsReducer<
290
+ Expand<ResolveFromParams<TRouteTree, TParamVariant, TFrom>>,
291
+ Expand<
292
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
293
+ >
294
+ > & {})
295
+ }
296
+ : {
297
+ params?:
298
+ | true
299
+ | (ParamsReducer<
300
+ Expand<ResolveFromParams<TRouteTree, TParamVariant, TFrom>>,
301
+ Expand<
302
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
303
+ >
304
+ > & {})
305
+ }
306
+
307
+ type MakeRequiredParamsReducer<
308
+ TRouteTree extends AnyRoute,
309
+ TParamVariant extends ParamVariant,
310
+ TFrom,
311
+ TToParams,
312
+ TFromParams = ResolveFromParams<TRouteTree, TParamVariant, TFrom>,
313
+ > =
314
+ | ([TFromParams] extends [WithoutEmpty<PickRequired<TToParams>>]
315
+ ? true
316
+ : never)
317
+ | ParamsReducer<Expand<TFromParams>, TToParams>
318
+
319
+ export type MakeRequiredParams<
320
+ TRouteTree extends AnyRoute,
321
+ TParamVariant extends ParamVariant,
322
+ TFrom,
323
+ TTo,
324
+ > = TParamVariant extends 'SEARCH'
325
+ ? {
326
+ search: Expand<
327
+ MakeRequiredParamsReducer<
328
+ TRouteTree,
329
+ TParamVariant,
330
+ TFrom,
331
+ Expand<ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>>
332
+ >
333
+ > & {}
334
+ }
335
+ : {
336
+ params: Expand<
337
+ MakeRequiredParamsReducer<
338
+ TRouteTree,
339
+ TParamVariant,
340
+ TFrom,
341
+ Expand<ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>>
342
+ >
343
+ > & {}
344
+ }
345
+
346
+ export type IsRequiredParams<TParams> = keyof TParams extends infer K extends
347
+ keyof TParams
348
+ ? K extends any
349
+ ? undefined extends TParams[K]
350
+ ? never
351
+ : true
352
+ : never
353
+ : never
354
+
355
+ export type IsRequired<
356
+ TRouteTree extends AnyRoute,
357
+ TParamVariant extends ParamVariant,
358
+ TFrom,
359
+ TTo,
360
+ > = string extends TTo
361
+ ? string extends TFrom
362
+ ? never
363
+ : IsRequiredParams<
364
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
365
+ >
366
+ : IsRequiredParams<
367
+ ResolveRelativeToParams<TRouteTree, TParamVariant, TFrom, TTo>
368
+ >
369
+
370
+ export type ParamOptions<
371
+ TRouteTree extends AnyRoute,
372
+ TFrom,
373
+ TTo extends string,
374
+ TParamVariant extends ParamVariant,
375
+ > =
376
+ IsRequired<TRouteTree, TParamVariant, TFrom, TTo> extends never
377
+ ? MakeOptionalParams<TRouteTree, TParamVariant, TFrom, TTo>
378
+ : MakeRequiredParams<TRouteTree, TParamVariant, TFrom, TTo>
270
379
 
271
380
  export type SearchParamOptions<
272
381
  TRouteTree extends AnyRoute,
@@ -283,9 +392,9 @@ export type PathParamOptions<
283
392
  export type ToPathOption<
284
393
  TRouteTree extends AnyRoute = AnyRoute,
285
394
  TFrom extends RoutePaths<TRouteTree> | string = string,
286
- TTo extends string = '',
395
+ TTo extends string = string,
287
396
  > =
288
- | TTo
397
+ | CheckPath<TRouteTree, TTo, never, TFrom, TTo>
289
398
  | RelativeToPathAutoComplete<
290
399
  TRouteTree,
291
400
  NoInfer<TFrom> extends string ? NoInfer<TFrom> : '',
@@ -317,14 +426,8 @@ export type LinkOptions<
317
426
  disabled?: boolean
318
427
  }
319
428
 
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
- }
429
+ export type CheckPath<TRouteTree extends AnyRoute, TPass, TFail, TFrom, TTo> =
430
+ ResolveRoute<TRouteTree, TFrom, TTo> extends never ? TFail : TPass
328
431
 
329
432
  export type ResolveRelativePath<TFrom, TTo = '.'> = TFrom extends string
330
433
  ? TTo extends string
@@ -634,7 +737,7 @@ export type ActiveLinkOptions<
634
737
  export type LinkProps<
635
738
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
636
739
  TFrom extends RoutePaths<TRouteTree> | string = string,
637
- TTo extends string = '',
740
+ TTo extends string = string,
638
741
  TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
639
742
  TMaskTo extends string = '',
640
743
  > = ActiveLinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
@@ -644,6 +747,23 @@ export type LinkProps<
644
747
  | ((state: { isActive: boolean }) => React.ReactNode)
645
748
  }
646
749
 
750
+ type LinkComponentProps<TComp> = React.PropsWithoutRef<
751
+ TComp extends React.FC<infer TProps> | React.Component<infer TProps>
752
+ ? TProps
753
+ : TComp extends keyof JSX.IntrinsicElements
754
+ ? Omit<React.HTMLProps<TComp>, 'children' | 'preload'>
755
+ : never
756
+ > &
757
+ React.RefAttributes<
758
+ TComp extends
759
+ | React.FC<{ ref: infer TRef }>
760
+ | React.Component<{ ref: infer TRef }>
761
+ ? TRef
762
+ : TComp extends keyof JSX.IntrinsicElements
763
+ ? React.ComponentRef<TComp>
764
+ : never
765
+ >
766
+
647
767
  export type LinkComponent<TComp> = <
648
768
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
649
769
  TFrom extends RoutePaths<TRouteTree> | string = string,
@@ -651,23 +771,8 @@ export type LinkComponent<TComp> = <
651
771
  TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
652
772
  TMaskTo extends string = '',
653
773
  >(
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
- >,
774
+ props: LinkProps<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
775
+ LinkComponentProps<TComp>,
671
776
  ) => React.ReactElement
672
777
 
673
778
  export function createLink<const TComp>(Comp: TComp): LinkComponent<TComp> {