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
automatic-azure•10mo ago
are you just looking for types? or also runtime logic?
how do you define a valid route?
yappiest-sapphireOP•10mo 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.
automatic-azure•10mo ago
can you give me a more detailed example please?
with a few valid routes and some input you want to validate against
yappiest-sapphireOP•10mo 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"
automatic-azure•10mo 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?
yappiest-sapphireOP•10mo ago
I need to use it as a function parameter
automatic-azure•10mo ago
can you show a full example of all of that?
automatic-azure•10mo 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 typeyappiest-sapphireOP•10mo ago
Okay, thank you very much!
automatic-azure•10mo 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
yappiest-sapphireOP•10mo ago
That would be much appreciated 😀
automatic-azure•10mo 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
automatic-azure•10mo ago
Please be aware that this uses internal types that might change
@Chris Horobin maybe this is another candidate for a type primitive?
yappiest-sapphireOP•10mo ago
Thank you so much!