@@ -159,51 +159,35 @@ export interface DataRouteObject extends RouteObject {
159
159
id : string ;
160
160
}
161
161
162
- type ParamParseFailed = { failed : true } ;
163
-
164
- type ParamParseSegment < Segment extends string > =
165
- // Check here if there exists a forward slash in the string.
166
- Segment extends `${infer LeftSegment } /${infer RightSegment } `
167
- ? // If there is a forward slash, then attempt to parse each side of the
168
- // forward slash.
169
- ParamParseSegment < LeftSegment > extends infer LeftResult
170
- ? ParamParseSegment < RightSegment > extends infer RightResult
171
- ? LeftResult extends string
172
- ? // If the left side is successfully parsed as a param, then check if
173
- // the right side can be successfully parsed as well. If both sides
174
- // can be parsed, then the result is a union of the two sides
175
- // (read: "foo" | "bar").
176
- RightResult extends string
177
- ? LeftResult | RightResult
178
- : LeftResult
179
- : // If the left side is not successfully parsed as a param, then check
180
- // if only the right side can be successfully parse as a param. If it
181
- // can, then the result is just right, else it's a failure.
182
- RightResult extends string
183
- ? RightResult
184
- : ParamParseFailed
185
- : ParamParseFailed
186
- : // If the left side didn't parse into a param, then just check the right
187
- // side.
188
- ParamParseSegment < RightSegment > extends infer RightResult
189
- ? RightResult extends string
190
- ? RightResult
191
- : ParamParseFailed
192
- : ParamParseFailed
193
- : // If there's no forward slash, then check if this segment starts with a
194
- // colon. If it does, then this is a dynamic segment, so the result is
195
- // just the remainder of the string, optionally prefixed with another string.
196
- // Otherwise, it's a failure.
197
- Segment extends `${string } :${infer Remaining } `
198
- ? Remaining
199
- : ParamParseFailed ;
162
+ type Star = "*"
163
+ /**
164
+ * @private
165
+ * Return string union from path string.
166
+ * @example
167
+ * PathParam<"/path/:a/:b"> // "a" | "b"
168
+ * PathParam<"/path/:a/:b/*"> // "a" | "b" | "*"
169
+ */
170
+ type PathParam <
171
+ Path extends string
172
+ > =
173
+ Path extends `:${infer Param } /${infer Rest } `
174
+ ? Param | PathParam < Rest >
175
+ : Path extends `:${infer Param } `
176
+ ? Param
177
+ : Path extends `${any } :${infer Param } `
178
+ ? PathParam < `:${Param } `>
179
+ : Path extends `${any } /${Star } `
180
+ ? Star
181
+ : Path extends Star
182
+ ? Star
183
+ : never
200
184
201
185
// Attempt to parse the given string segment. If it fails, then just return the
202
186
// plain string type as a default fallback. Otherwise return the union of the
203
187
// parsed string literals that were referenced as dynamic segments in the route.
204
188
export type ParamParseKey < Segment extends string > =
205
- ParamParseSegment < Segment > extends string
206
- ? ParamParseSegment < Segment >
189
+ [ PathParam < Segment > ] extends [ never ]
190
+ ? PathParam < Segment >
207
191
: string ;
208
192
209
193
/**
@@ -441,24 +425,6 @@ function matchRouteBranch<
441
425
return matches ;
442
426
}
443
427
444
- /**
445
- * @private
446
- * Parameters in the path
447
- */
448
- type PathParam <
449
- Path extends string
450
- > = Path extends `:${infer Param } /${infer Rest } `
451
- ? Param | PathParam < Rest >
452
- : Path extends `:${infer Param } `
453
- ? Param
454
- : Path extends `${any } :${infer Param } `
455
- ? PathParam < `:${Param } `>
456
- : Path extends `${any } /*`
457
- ? "*"
458
- : Path extends "*"
459
- ? "*"
460
- : never
461
-
462
428
/**
463
429
* Returns a path with params interpolated.
464
430
*
0 commit comments