TanStackT
TanStack12mo ago
16 replies
efficient-indigo

Tanstack router looses types in loader

I am developing a SPA using Tanstack router and convex.dev.

The following code works and
result
is properly typed inside the loader function. In my route component, when I run const data = Route.useLoaderData(); the TypeScript type is
any
.

Am I using the loader function wrong here? I've tried adding an explicit return type, but it still didn't work

export const Route = createFileRoute(
  "/_app/freelance/my-applications/_layout/$applicationId",
)({
  component: RouteComponent,
  loader: async ({
    context,
    params,
  }: {
    context: { queryClient: QueryClient };
    params: { applicationId: Id<"applications"> };
    }): Promise<{
      application: Application;
      job: Job;
    }> => {
    const result = await context.queryClient.ensureQueryData(
      convexQuery(api.app.applications.getApplication, {
        applicationId: params.applicationId,
      }),
    );
    if (!result?.application) {
      throw new Error(
        `Application with ID "${params.applicationId}" not found.`,
      );
    }
    return result;
  },
});

function RouteComponent() {
  const data = Route.useLoaderData();

  return (
    <div>
      <h1>Page</h1>
    </div>
  );
}
Was this page helpful?