react-router icon indicating copy to clipboard operation
react-router copied to clipboard

[Bug]: tsc doesn't check if second argument was provided for generatePath

Open gloomweaver opened this issue 2 years ago • 4 comments

What version of React Router are you using?

6.8.1

Steps to Reproduce

When using generatePath without providing parameters typescript doesn't throw an error. generatePath('hello/:world');

Expected Behavior

I would expect for typescript to show me that I should provide second argument with 'world' property.

Actual Behavior

Tsc doesn't throw any error.

gloomweaver avatar Feb 13 '23 16:02 gloomweaver

function generatePathStrict<Path extends string>(
  originalPath: Path,
  ...params: PathParam<Path> extends never
    ? [object?]
    : [
        {
          [key in PathParam<Path>]: string | null;
        },
      ]
) {
  return generatePath(originalPath, params);
}

This definition works for me

gloomweaver avatar Feb 13 '23 16:02 gloomweaver

This issue has been automatically marked stale because we haven't received a response from the original author in a while 🙈. This automation helps keep the issue tracker clean from issues that are not actionable. Please reach out if you have more information for us or you think this issue shouldn't be closed! 🙂 If you don't do so within 7 days, this issue will be automatically closed.

github-actions[bot] avatar Apr 17 '23 20:04 github-actions[bot]

I opened a MR that fixes this issue. It's awaiting approval. Don't close.

gloomweaver avatar Apr 17 '23 21:04 gloomweaver

WTF I'm actually having the opposite issue. A path with an optional argument on it is making TSC to fail because it's expecting such argument.

I have an enum with all my routes defined, among them I have this one /organizations/:pid/:page?. I would expect genreatePath to not force me to add page since it's set as optional, but TSC complains about it:

// RoutePath.Organization is "/organizations/:pid/:page?"
generatePath(RoutePath.Organization, { pid: entity })

TSC outputs:

Argument of type '{ pid: string; }' is not assignable to parameter of type '{ page: string | null; pid: string | null; }'.
  Property 'page' is missing in type '{ pid: string; }' but required in type '{ page: string | null; pid: string | null; }'.ts(2345)

I would expect optional params to not be mandatory.

elboletaire avatar Jul 05 '24 08:07 elboletaire