T
TanStack11mo ago
stormy-gold

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
unwilling-turquoise11mo ago
TanStack | 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.
From An unknown user
From An unknown user
stormy-gold
stormy-goldOP11mo 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:
params: {
parse: ({ entity }: { entity: Entity }) => ({ entity }),
stringify: ({ entity }: { entity: Entity }) => ({ entity }),
}
params: {
parse: ({ entity }: { entity: Entity }) => ({ entity }),
stringify: ({ entity }: { entity: Entity }) => ({ entity }),
}
is there another way to achieve the same type assertion for params without passing a reflective 'boilerplate' function?
unwilling-turquoise
unwilling-turquoise11mo 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 string
magic-amber
magic-amber11mo ago
exactly. I would use something like zod for this
stormy-gold
stormy-goldOP11mo ago
Ah I see, good suggestion

Did you find this page helpful?