-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
feat: improve generatePath arg type #9090
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
feat: improve generatePath arg type #9090
Conversation
|
Hi @tyankatsu0105, Welcome, and thank you for contributing to React Router! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run. Thanks! - The Remix team |
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
packages/router/utils.ts
Outdated
? "*" | ||
: Path extends "*" | ||
? "*" | ||
: 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.
I'll preface this with the fact that I'm no TS expert :). But It feels like this is doing something very similar (if not the same thing) as ParamParseSegment
/Params
higher up in this file? Do you think there's any chance to leverage a shared type?
Also going to ping @pcattori on this one as our in-house TS guru
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.
To my eyes, this looks like a simpler and more complete version of ParamParseSegment
(more complete, because it accounts for "*"
params).
It also returns either a union of string
or just never
when parsing fails (instead of ParamParseSegment
's type ParamParseFailed = { failed: true }
) , so its suitable to use directly as a property key.
If we can migrate or rewrite the comments from ParamParseSegment
to PathParams
, I'd be in favor of replacing ParamParseSegment
with this.
Also we'd need to refactor ParamParseKey
to use PathParams
:
export type ParamParseKey<Segment extends string> =
[PathParams<Segment>] extends [never]
? string
: PathParams<Segment>;
*corrected^
Action items:
- Move comments from
ParamParseSegment
toPathParams
- Remove
ParamParseSegment
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.
Naming-wise, I think singular nouns work better for unions. So PathParam
feels better to me. Like "a path param can be any of these". But not a strong opinion there.
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 think singular nouns work better for unions
I totally agree. To be honest, I don't have an opinion on the naming. So, this opinion looks good to me.
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.
@pcattori
I did all tasks.
Please review my feature.
export type ParamParseKey<Segment extends string> = | ||
ParamParseSegment<Segment> extends string | ||
? ParamParseSegment<Segment> | ||
[PathParam<Segment>] extends [never] | ||
? PathParam<Segment> | ||
: string; |
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 think we want to swap the branches here:
export type ParamParseKey<Segment extends string> =
[PathParam<Segment>] extends [never]
? string
: PathParam<Segment>;
@tyankatsu0105 I'm going to merge this as-is and continue the work in a separate PR so that we can get these improvements into our next prerelease. Thank you for your work! |
What
This PR improves the
params
type ofgeneratePath
's argument.You can try on TS Playground