-
Notifications
You must be signed in to change notification settings - Fork 8
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
A $safeParseParams
which doesn't raise an error immediately
#58
Comments
Some code for reference: export type InferParams<RP> =
RP extends RoutesProps<infer _R, infer _C, infer Params>
? {
params: Params["path"];
searchParams: Params["query"];
}
: never;
export function $safeParseParams<
R extends RouteNodeMap,
C extends AnyRenderContext,
P extends ParamRecordMap = ParamRecordMap,
>(routes: RoutesProps<R, C, P>, pathname: string) {
try {
return routes.$parseParams(pathname);
} catch {
return null;
}
} |
Thank you for the example. I already had this on my mind. It could be done by adding a meta prop like |
I just came up with an idea. However, I can't compare it with your solution because I don't understand the purpose of The following snippet defines a generic function safeCall<T extends (...args: any[]) => any>(
fn: T,
...params: Parameters<T>
):
| { success: true; result: ReturnType<T> }
| { success: false; error: Error } {
try {
const result = fn(...params);
return {
success: true,
result,
};
} catch (error) {
return {
success: false,
error,
};
}
}
safeCall(routes.$parseParams, pathname); This type of wrapper is probably already included in some utility library. I'll need to check. If not, I may have to bundle it with typesafe-routes. edit: |
I think the implementation for
Ah yeah, my bad. I needed it to type the |
I found lodash attempt and several other similar utility functions. However they all have very loose Typescript types. So, I decided to include
Oh, I see. That's nice. We had a similar utility type in the previous versions of typesafe-routes. Would you mind if I incorporate your code into the library? I would rather not consider the type The property names |
I would not mind at all, please feel free to add it!
Yes, I am using this within a Next.js project. export type InferParams<RP> =
RP extends RoutesProps<infer _R, infer _C, infer Params>
? Params
: never;
I was slightly wrong here; it should be |
I just ran a few tests, and with TypeScript < 5.0.0, For versions 4.5-4.9, the parameters can be inferred as follows: type InferredParams = {
query: ReturnType<typeof r.parent.child.grandchild.$parseQuery>;
path: ReturnType<typeof r.parent.child.grandchild.$parseParams>;
} This isn't as convenient as the utility function, but at least it's compatible with older versions. This is just for documentation purposes, in case someone looks for the information in the issues. The workaround will be included in the documentation. I'm closing this issue and will publish a new version based on the results of this discussion. Thanks again. ✌️ |
I find myself using
$parseParams
when I need to check if the current pathname matches a certain route handler (for rendering components in a common layout).I currently use a
try
andcatch
wrapper for this, but I thought it would be a nice addition to this useful library.The text was updated successfully, but these errors were encountered: