Capítulo 24 de 57
The linkOptions() helper type-checks an object literal intended for Link/navigate/redirect eagerly (at definition time, not at usage time) and returns it unchanged, making navigation option objects safely reusable across multiple call sites.
{ to: '/dashboard', search: { search: '' } } has to inferred as string, which loosely matches any route, and type errors in it only surface once spread into <Link>, not at the point of definition.linkOptions(options): A function that type-checks its object-literal argument against the same types Link uses, and returns the input as-is (fully inferred), so the object is safe and portable wherever ToOptions-shaped input is expected (Link props, navigate(), redirect()).linkOptions() result can be spread into <Link {...opts} />, passed to navigate(opts), or thrown via redirect(opts) (e.g. inside a route's beforeLoad), all sharing one type-checked source of truth.linkOptions([{ to, label, ... }, ...]) type-checks an array of option objects (e.g. for building a nav bar), while still allowing extra non-Link properties (like a custom label) to be inferred and used at the call site.const dashboardLinkOptions = linkOptions({
to: '/dashboard',
search: { search: '' },
})
export const Route = createFileRoute('/dashboard')({
component: DashboardComponent,
validateSearch: (input) => ({ search: input.search }),
beforeLoad: () => {
// can be used in redirect
throw redirect(dashboardLinkOptions)
},
})
function DashboardComponent() {
const navigate = useNavigate()
return (
<div>
<button onClick={() => navigate(dashboardLinkOptions)} />
<Link {...dashboardLinkOptions} />
</div>
)
}
linkOptions() object reused across redirect, navigate, and Link.linkOptions() instead of plain object literals, it catches type errors at definition time rather than at every usage site.linkOptions() accepts an array too, which is the idiomatic way to type-check a data-driven navigation menu while keeping extra display-only fields (like label) available.linkOptions() output is valid input for Link, useNavigate()'s returned function, and redirect(), since they all share the ToOptions/NavigateOptions type family.ToOptions/NavigateOptions/LinkOptions interfaces that linkOptions() type-checks against.