@@ -146,7 +146,7 @@ export type RouterApi<T extends Routes> = {
146
146
*/
147
147
params : AllParams < T > ;
148
148
/** The reactive pathname of the URL. */
149
- pathname : Path < T > ;
149
+ pathname : ( Path < T , true > & { } ) | ( string & { } ) ;
150
150
/** The reactive query string part of the URL. */
151
151
search : string ;
152
152
/** The reactive history state that can be passed to the `navigate` function. */
@@ -156,8 +156,8 @@ export type RouterApi<T extends Routes> = {
156
156
} ;
157
157
} ;
158
158
159
- export type Path < T extends Routes > = RemoveParenthesis <
160
- RemoveLastSlash < RecursiveKeys < StripNonRoutes < T > > >
159
+ export type Path < T extends Routes , AnyParam extends boolean = false > = RemoveParenthesis <
160
+ RemoveLastSlash < RecursiveKeys < StripNonRoutes < T > , '' , AnyParam > >
161
161
> ;
162
162
163
163
export type ConstructPathArgs < T extends string > =
@@ -167,9 +167,13 @@ export type IsActiveArgs<T extends string> =
167
167
PathParams < T > extends never ? [ T ] : [ T ] | [ T , PathParams < T > ] ;
168
168
169
169
export type PathParams < T extends string > =
170
- ExtractParams < T > extends never ? never : Record < ExtractParams < T > , string > ;
170
+ ExtractParams < RemoveParenthesis < T > > extends never
171
+ ? never
172
+ : Record < ExtractParams < RemoveParenthesis < T > > , string > ;
171
173
172
- export type AllParams < T extends Routes > = Partial < Record < ExtractParams < RecursiveKeys < T > > , string > > ;
174
+ export type AllParams < T extends Routes > = Partial <
175
+ Record < ExtractParams < RemoveParenthesis < RecursiveKeys < T > > > , string >
176
+ > ;
173
177
174
178
export type NavigateOptions =
175
179
| {
@@ -207,14 +211,28 @@ type StripNonRoutes<T extends Routes> = {
207
211
: K ] : T [ K ] extends Routes ? StripNonRoutes < T [ K ] > : T [ K ] ;
208
212
} ;
209
213
210
- type RecursiveKeys < T extends Routes , Prefix extends string = '' > = {
214
+ type RecursiveKeys <
215
+ T extends Routes ,
216
+ Prefix extends string = '' ,
217
+ AnyParam extends boolean = false ,
218
+ > = {
211
219
[ K in keyof T ] : K extends string
212
220
? T [ K ] extends Routes
213
- ? RecursiveKeys < T [ K ] , `${Prefix } ${K } `>
214
- : `${Prefix } ${K } `
221
+ ? RecursiveKeys <
222
+ T [ K ] ,
223
+ `${Prefix } ${AnyParam extends true ? ReplaceParamWithString < K > : K } `,
224
+ AnyParam
225
+ >
226
+ : `${Prefix } ${AnyParam extends true ? ReplaceParamWithString < K > : K } `
215
227
: never ;
216
228
} [ keyof T ] ;
217
229
230
+ type ReplaceParamWithString < T extends string > = T extends `/:${string } `
231
+ ? `/${string } `
232
+ : T extends `/(:${string } )`
233
+ ? `/${string } `
234
+ : T ;
235
+
218
236
type RemoveLastSlash < T extends string > = T extends '/' ? T : T extends `${infer R } /` ? R : T ;
219
237
220
238
type RemoveParenthesis < T extends string > = T extends `${infer A } (${infer B } )${infer C } `
@@ -223,14 +241,10 @@ type RemoveParenthesis<T extends string> = T extends `${infer A}(${infer B})${in
223
241
224
242
type ExtractParams < T extends string > = T extends `${string } :${infer Param } /${infer Rest } `
225
243
? Param | ExtractParams < `/${Rest } `>
226
- : T extends `${string } ( :${infer Param } ) `
244
+ : T extends `${string } :${infer Param } `
227
245
? Param
228
- : T extends `${string } :${infer Param } `
229
- ? Param
230
- : T extends `${string } (*${infer Param } )`
231
- ? Param
232
- : T extends `${string } *${infer Param } `
233
- ? Param extends ''
234
- ? never
235
- : Param
236
- : never ;
246
+ : T extends `${string } *${infer Param } `
247
+ ? Param extends ''
248
+ ? never
249
+ : Param
250
+ : never ;
0 commit comments