While using the quite new tanstack router, i encountered some strange TypeScript behavior, one being the property to
(from Router.navigate
) being aliased to any
when accessed using the Parameters<T>
type built-in.
[src/main.tsx]
The second one happens at any route. I created a function with the following signature (full function here):
function artificialDelay<T>(ms = 5000): (val?: T) => Promise<T | undefined>
Using this function in the RouteOptions.beforeLoad
like this:
createRootRoute({
beforeLoad: artificialDelay(),
component: () => (<></>),
});
Produces an gigantic error that can be summed up to the following:
Types of parameters
val
andopts
are incompatible.{ search: RootSearchSchema; abortController: AbortController; preload: boolean; params: {}; context: {}; location: ParsedLocation<{}>; navigate: NavigateFn; buildLocation: BuildLocationFn<...>; cause: "preload" | /* 1 more */ | "stay"; }
is not assignable to type
{ search: never; abortController: AbortController; preload: boolean; params: {}; context: never; location: ParsedLocation<{}>; navigate: NavigateFn; buildLocation: BuildLocationFn<...>; cause: "preload" | /* 1 more */ | "stay"; }
Types of property
search
are incompatible.Type
RootSearchSchema
is not assignable to typenever
.
You can see a minimal reproduction case here, based in the Quickstart (file-based) guide provided at the tanstack router docs page
-
Router.navigate
, propertyto
being aliased toany
here -
RouteOptions.beforeLoad
opts.search
being aliased tonever
here -
artificialDelay
function declaration here
As much as I wanted to, this seems to be a bug in TypeScript, not a bug in the tanstack router.
I tried multiple ways to get the parameters type, with all of them resulting in any
being returned.
In the function generic, it shouldn’t modify any of the parameters, returning the same type it received.