Skip to content

Conversation

tyankatsu0105
Copy link
Contributor

@tyankatsu0105 tyankatsu0105 commented Jul 22, 2022

What

This PR improves the params type of generatePath's argument.

type Star = "*"
type PathParam<
 Path extends string
> = Path extends `:${infer Param}/${infer Rest}`
   ? Param | PathParam<Rest>
   : Path extends `:${infer Param}`
     ? Param
     : Path extends `${any}/:${infer Param}`
         ? PathParam<`:${Param}`>
         : Path extends `${any}/${Star}`
           ? Star
           : Path extends Star
             ? Star 
             : never
export function generatePath<Path extends string>(path: Path, params: {
  [key in PathParam<Path>]: string
} = {} as any): string {
  return ""
}

generatePath("a", {}) // {}
//^?

generatePath(":a", {})// {"a": string;}
//^?

generatePath("/:a", {})// {"a": string;}
//^?

generatePath("/:a/b", {})// {"a": string;}
//^?

generatePath("/:a/:b", {})// {"a": string; "b": string;}
//^?

generatePath("/a/:b", {})// {"b": string;}
//^?

generatePath("/:a/b/:c", {})// {"a": string; "c": string;}
//^?

generatePath("*", {})// {"*": string;}
//^?

generatePath("/*", {})// {"*": string;}
//^?

generatePath("/a/*/b", {})// {}
//^?

generatePath("/a/b/*", {})// {"*": string;}
//^?

You can try on TS Playground

@changeset-bot
Copy link

changeset-bot bot commented Jul 22, 2022

⚠️ No Changeset found

Latest commit: e6f0f64

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jul 22, 2022

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 CLA Signed label will be added to the pull request.

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

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jul 22, 2022

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@timdorr timdorr requested a review from brophdawg11 July 24, 2022 01:35
? "*"
: Path extends "*"
? "*"
: never
Copy link
Contributor

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

Copy link
Contributor

@pcattori pcattori Jul 27, 2022

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 to PathParams
  • Remove ParamParseSegment

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Comment on lines 193 to 196
export type ParamParseKey<Segment extends string> =
ParamParseSegment<Segment> extends string
? ParamParseSegment<Segment>
[PathParam<Segment>] extends [never]
? PathParam<Segment>
: string;
Copy link
Contributor

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>;

@pcattori
Copy link
Contributor

pcattori commented Aug 1, 2022

@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!

@pcattori pcattori merged commit 578cc0b into remix-run:dev Aug 1, 2022
@tyankatsu0105 tyankatsu0105 deleted the feat/imp-generatePath-type branch August 2, 2022 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants