Feature Request Filtered Route list by parameter names #625
Replies: 3 comments 4 replies
-
Sounds like what you're looking for is a type utility that narrows RouteMap to only contain routes that contain a parameter called slug: type RouteNamesWhereParams<Params extends RouteParamsRawGeneric> = {
[Name in keyof RouteMap]: RouteMap[Name] extends RouteRecordInfo<string, string, Params> ? Name : never;
}[keyof RouteMap];
type RoutesWithSlug = RouteNamesWhereParams<{ slug: any }>; |
Beta Was this translation helpful? Give feedback.
-
That's pretty cool! 🙌 It seems to me that you need a type narrowing, like the one @PindaPixel posted. BTW, you are supposed to call |
Beta Was this translation helpful? Give feedback.
-
I have my solution, but it is kindof an inverse of this discussion title though:
I don't know if this would be a recommended approach, if this should be documented somewhere, or what any other next steps should be. As is, the code meets my needs. Let me know if there's something I can do to help. These dataLoaders are exceptional, and I'd love to give back in any way. To which, again, these loaders are very fast and very flexible. It didn't seem to matter at all that I have a lot of different tools being used. Implementing a functional system only took me a few hours of effort. (Getting the types was a pain, but mainly due to me trying to force it to work a different way). I look forward to when the feature is stable, because again, it is an amazing feaure. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I just started experimenting with adding data-loaders, and I have to say that they are amazing!
I now have 2 composables that I can put in 90% of my views to simplify the initial data queries. Combine that with pinia-colada, and my page loading is atleast twice as fast (so fast... the overhead in my stress test is the majority of my test time).
A nice to have would be a way to get a RouteMap or similar type, that only contains the subset or routes that provide the specified parameter.
Here is my default fetch composable:
Right now, my code throws some IDE errors, as there isn't any definitions stating that
config.key
would be present in theto.params
object. While I could do some null checks, my ideal setup would be to have a type mapping such that if I specify thatconfig.key
isslug
, only routes with[slug]
are compatible withname
. Furthermore, if I specify thatconfig.key
isid
, then only routes with[id]
are compatible withname
.I'll do some experimentation on my end, and see what I can make. If I come up with a strong alternative solution I'll post it on my side.
Rationale
The reasoning behind my composable above is that most of my data fetching is the same exact call, with just a few variations. These variations are either the resource type, the resource id, or the related resources to load with the resource.
I already thing I am going to restructure my arguments to be something such as:
Final notes
This issue isn't critical to me, I just figure it may be a nice to have, especially in conjunction with data loaders. In the past I was fine because the query was tied to the same context as the page setup. As for now, I will probably just ignore the IDE errors. If I do get some type definitions running well, I can add them to this issue (even if closed).
Beta Was this translation helpful? Give feedback.
All reactions