-
-
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
Conversation
? TPath['params'] | ||
: TPath extends string | ||
? ExtractParamsFromPathString<UnifyParamEnds<TPath>> | ||
? Path<TPath, {}>['params'] | ||
: never |
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.
TPath extends string | ||
> = ReplaceAll<ReplaceAll<TPath, '-', ParamEnd>, '_', ParamEnd> | ||
|
||
export type ExtractParamsFromPathString< | ||
TPath extends string, | ||
TParams extends Record<string, Param> = Record<never, never> | ||
TParams extends Record<string, Param | undefined> = Record<never, never> |
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.
These | undefined
additions just account for the user defined param types being an optional record which adds | undefined
to every property.
@@ -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 comment
The 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.
> = 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}` |
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.
I moved wrapping the path in UnifyParamEnds
into this type so that we don't have to remember wrap any path string we pass into this type in it.
Description
Path originally just returned the same types as its arguments. Which was never really correct but allowed for all the other types to be written. Implementing the type path should actually return by having all route method types use Path and having Path be responsible for extracting param types.