T
TanStack4mo ago
genetic-orange

Is this use of FileRouteTypes['to'] valid?

Hello, I'm using a Link component and the to prop is dynamic. I get the value of the to prop by calling my getConfigureModeLink function. To ensure the function can only return a valid registered route I have typed it's return type as FileRouteTypes['to'] which is the interface exported from routeTree.gen.ts. My Link component:
<Link
title="Configure Mode"
to={getConfigureModeLink()}
>
// ...
</Link>
<Link
title="Configure Mode"
to={getConfigureModeLink()}
>
// ...
</Link>
The getConfigureModeLink function:
const getConfigureModeLink = (): FileRouteTypes['to'] => {
let link: FileRouteTypes['to'] = '/'
if (displaySettings?.mode === DisplayMode.SLIDESHOW) {
link = '/slideshow-configuration'
} else if (displaySettings?.mode === DisplayMode.IMAGE_FEED) {
link = '/image-feed-configuration'
}
return link;
}
const getConfigureModeLink = (): FileRouteTypes['to'] => {
let link: FileRouteTypes['to'] = '/'
if (displaySettings?.mode === DisplayMode.SLIDESHOW) {
link = '/slideshow-configuration'
} else if (displaySettings?.mode === DisplayMode.IMAGE_FEED) {
link = '/image-feed-configuration'
}
return link;
}
Question This works fine. What I would like to know is: Is this how I should be typing a variable to ensure it is a valid route or is there a more conventional approach? Thank You.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?