How do I assert types for route params?
when I declare a new file route, such as:
createFileRoute("/listening/top/$entity")
is it possible to assert or assign a type to $entity
? in my case I'd like for it to be an enum with artists
, tracks
, albums
but I can't figure out how to extend it beyond a string
5 Replies
unwilling-turquoise•11mo ago
you can with
params.parse/stringify
https://tanstack.com/router/latest/docs/framework/react/api/router/RouteOptionsType#paramsparse-methodTanStack | High Quality Open-Source Software for Web Developers
Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.

stormy-goldOP•11mo ago
awesome, thanks! is there a code example as well?
no worries re: code example, figured out that
params.parse/stringify
means params: { parse: record => ({}), stringify: record ({})}
that said, this object seems a bit silly considering it's more/less returning the fn signature object, just typed:
is there another way to achieve the same type assertion for params without passing a reflective 'boilerplate' function?unwilling-turquoise•11mo ago
not that I'm aware of
it might seem less silly if you run assertion code to verify that
entity
is actually a valid Entity
unless it can be any random stringmagic-amber•11mo ago
exactly. I would use something like zod for this
stormy-goldOP•11mo ago
Ah I see, good suggestion