Skip to content

Commit a786f8a

Browse files
committed
refactor: remove ParamParseSegment and use PathParam
1 parent 994b9c0 commit a786f8a

File tree

2 files changed

+26
-60
lines changed

2 files changed

+26
-60
lines changed

docs/hooks/use-match.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ title: useMatch
88
<summary>Type declaration</summary>
99

1010
```tsx
11-
declare function useMatch<ParamKey extends string = string>(
12-
pattern: PathPattern | string
11+
declare function useMatch<ParamKey extends ParamParseKey<Path>, Path extends string>(
12+
pattern: PathPattern<Path> | Path
1313
): PathMatch<ParamKey> | null;
1414
```
1515

packages/router/utils.ts

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -159,51 +159,35 @@ export interface DataRouteObject extends RouteObject {
159159
id: string;
160160
}
161161

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
200184

201185
// Attempt to parse the given string segment. If it fails, then just return the
202186
// plain string type as a default fallback. Otherwise return the union of the
203187
// parsed string literals that were referenced as dynamic segments in the route.
204188
export type ParamParseKey<Segment extends string> =
205-
ParamParseSegment<Segment> extends string
206-
? ParamParseSegment<Segment>
189+
[PathParam<Segment>] extends [never]
190+
? PathParam<Segment>
207191
: string;
208192

209193
/**
@@ -441,24 +425,6 @@ function matchRouteBranch<
441425
return matches;
442426
}
443427

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-
462428
/**
463429
* Returns a path with params interpolated.
464430
*

0 commit comments

Comments
 (0)