react-router
react-router copied to clipboard
[Bug]: tsc doesn't check if second argument was provided for generatePath
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.
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
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.
I opened a MR that fixes this issue. It's awaiting approval. Don't close.
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.