T
TanStack17mo ago
sensitive-blue

Is there a exported type that returns all available absolute routes?

Hi! I have this example where i have an array of objects used for a long list of links:
interface NavTabType {
name: string;
href: TypeThatReturnsAllRoutes?;
}

const Navigation: NavTabType[] = [
{ name: "API keys", href: "/path/to/api", },
{ name: "Account", href: "/path/to/account" }
];
interface NavTabType {
name: string;
href: TypeThatReturnsAllRoutes?;
}

const Navigation: NavTabType[] = [
{ name: "API keys", href: "/path/to/api", },
{ name: "Account", href: "/path/to/account" }
];
Currently href has the string type assigned but is there a type that i can assign it that returns all available absolute routes?
3 Replies
rare-sapphire
rare-sapphire17mo ago
You'd have to pass in the necessary generics, but the ToPathOption type should work for you. Heres the source for it: https://github.com/TanStack/router/blob/a4a5b5e56879c7e27107bc9ac6408ce7c15207a6/packages/react-router/src/link.tsx#L391 You'd have to call it as such:
type Foo = ToPathOption<
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
TTo extends string = string,
>
type Foo = ToPathOption<
TRouter extends AnyRouter = RegisteredRouter,
TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
TTo extends string = string,
>
GitHub
router/packages/react-router/src/link.tsx at a4a5b5e56879c7e27107bc...
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - TanStack/router
sensitive-blue
sensitive-blueOP17mo ago
Thanks!
rising-crimson
rising-crimson16mo ago
What did you end up using? Are these still necessary?

Did you find this page helpful?