-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Path types #16
Path types #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,9 +58,9 @@ export type ExtractRouteMethodParams<T> = T extends RouteMethod<infer Params> | |
export type ExtractParamsFromPath< | ||
TPath extends Route['path'] | ||
> = TPath extends Path | ||
? ExtractParamsFromPathString<UnifyParamEnds<TPath['path']>, TPath['params']> | ||
? TPath['params'] | ||
: TPath extends string | ||
? ExtractParamsFromPathString<UnifyParamEnds<TPath>> | ||
? Path<TPath, {}>['params'] | ||
: never | ||
|
||
type ParamEnd = '/' | ||
|
@@ -71,12 +71,12 @@ type UnifyParamEnds< | |
|
||
export type ExtractParamsFromPathString< | ||
TPath extends string, | ||
TParams extends Record<string, Param> = Record<never, never> | ||
> = TPath extends `${infer Path}${ParamEnd}` | ||
TParams extends Record<string, Param | undefined> = Record<never, never> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These |
||
> = UnifyParamEnds<TPath> extends `${infer Path}${ParamEnd}` | ||
? ExtractParamsFromPathString<Path, TParams> | ||
: TPath extends `${string}:${infer Param}${ParamEnd}${infer Rest}` | ||
: UnifyParamEnds<TPath> extends `${string}:${infer Param}${ParamEnd}${infer Rest}` | ||
? MergeParams<{ [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams> }, ExtractParamsFromPathString<Rest, TParams>> | ||
: TPath extends `${string}:${infer Param}` | ||
: UnifyParamEnds<TPath> extends `${string}:${infer Param}` | ||
Comment on lines
+75
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved wrapping the path in |
||
? { [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams> } | ||
: Record<never, never> | ||
|
||
|
@@ -115,7 +115,7 @@ type ExtractParamName< | |
|
||
type ExtractPathParamType< | ||
TParam extends string, | ||
TParams extends Record<string, Param> | ||
TParams extends Record<string, Param | undefined> | ||
> = TParam extends `?${infer OptionalParam}` | ||
? OptionalParam extends keyof TParams | ||
? ExtractParamType<TParams[OptionalParam]> | undefined | ||
|
@@ -124,7 +124,7 @@ type ExtractPathParamType< | |
? ExtractParamType<TParams[TParam]> | ||
: string | ||
|
||
type ExtractParamType<TParam extends Param> = TParam extends ParamGetSet<infer Type> | ||
type ExtractParamType<TParam extends Param | undefined> = TParam extends ParamGetSet<infer Type> | ||
? Type | ||
: TParam extends ParamGetter | ||
? ReturnType<TParam> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ export type Path< | |
P extends PathParams<T> = any | ||
> = { | ||
path: T, | ||
params: P, | ||
params: Identity<ExtractParamsFromPathString<T, P>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the implementation of path params. Essentially just moving the ExtractParamsFromPathString type here rather than using it in the route method types. |
||
} | ||
|
||
export function path<T extends string, P extends PathParams<T>>(_path: T, _params: Identity<P>): Path<T, P> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is where you can see rather than extracting param types it just accepts what Path returns for the params type.