TanStackT
TanStack3mo ago
3 replies
uncertain-scarlet

is it possible to create a type-safe utility for beforeLoad?

i'm trying to create an Effect wrapper for beforeLoad (and maybe others). Basically there's just some reusable logic that i want in a wrapper, then i can just provide that directly to the beforeLoad property. i have it mostly figured out, but am unable to get inference of the function arg

e.g. (remove the effect piece for simplicity)
export function runFunction<TOptions>(fn: (options: TOptions) => unknown) {
  return (options: TOptions) => {
    return fn(options);
  };
}

// usage
export const Route = createFileRoute("/_application")({
  ssr: false,
  component: RouteComponent,
  beforeLoad: runFunction(function (options) { // options -> unknown
    return { abc: '123' };
  }),
})


i'm wondering if, given the level of inference going on, is something like this even possible?

alternative is to instead wrap in an inline function and get the args from there, but wondering if this ^ is even possible
Was this page helpful?