Skip to content
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

Merged
merged 2 commits into from
Dec 18, 2023
Merged

Path types #16

merged 2 commits into from
Dec 18, 2023

Conversation

pleek91
Copy link
Contributor

@pleek91 pleek91 commented Dec 17, 2023

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.

Comment on lines +61 to 64
? TPath['params']
: TPath extends string
? ExtractParamsFromPathString<UnifyParamEnds<TPath>>
? Path<TPath, {}>['params']
: never
Copy link
Contributor Author

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>
Copy link
Contributor Author

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>>,
Copy link
Contributor Author

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.

Comment on lines +75 to +79
> = 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}`
Copy link
Contributor Author

@pleek91 pleek91 Dec 17, 2023

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.

@pleek91 pleek91 merged commit c5b72c5 into main Dec 18, 2023
3 checks passed
@pleek91 pleek91 deleted the path-types branch December 18, 2023 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants