Routes with specific search params validator
How would I got about defining a type that accepts any of my routes, but with a specific search params valdidator? I use Zod for the validator if that matters.
15 Replies
ambitious-aqua•13mo ago
are you just looking for types? or also runtime logic?
how do you define a valid route?
flat-fuchsiaOP•13mo ago
I just need a type that allows me to pass any route with a specific search param validator, so the Route objects that are exported by each route.
So a valid route would be any routes in my app, that has the correct search param validator.
ambitious-aqua•13mo ago
can you give me a more detailed example please?
with a few valid routes and some input you want to validate against
flat-fuchsiaOP•13mo ago
Sure.
I made an example on StackBlitz: https://stackblitz.com/edit/tanstack-router-qldubm?file=src%2Ftype-example.ts,src%2Froutes%2Fabout.tsx,src%2Froutes%2Findex.tsx,src%2Froutes%2Fposts.tsx&preset=node
The main ideas are outlined in "type-example.ts"
ambitious-aqua•13mo ago
and what would you do that type?
I just see that you want to check that a route has a specific search schema
but what then?
flat-fuchsiaOP•13mo ago
I need to use it as a function parameter
ambitious-aqua•13mo ago
can you show a full example of all of that?
ambitious-aqua•13mo ago
we generally recommend not importing the Route as it is very easy to create circular dependencies where typescript will bail out
instead, we recommend using
getRouteApi
so what you would need is passing a routeId to a function and that should then validate the search typeflat-fuchsiaOP•13mo ago
Okay, thank you very much!
ambitious-aqua•13mo ago
but you would need a type for that
I have built something like that
it was not very efficient in terms of type performance ...
I'll create an example for you later
flat-fuchsiaOP•13mo ago
That would be much appreciated 😀
ambitious-aqua•13mo ago
here you go: https://stackblitz.com/edit/tanstack-router-i1ainz?file=src%2Ftype-example.ts&preset=node
Manuel Schiller
StackBlitz
Router Quickstart File Based Example (forked) - StackBlitz
Run official live example code for Router Quickstart File Based, created by Tanstack on StackBlitz
ambitious-aqua•13mo ago
Please be aware that this uses internal types that might change
@Chris Horobin maybe this is another candidate for a type primitive?
flat-fuchsiaOP•13mo ago
Thank you so much!