router icon indicating copy to clipboard operation
router copied to clipboard

ValidateLinkOptions throwing type error when trying to add search params through function.

Open alex-delia opened this issue 3 months ago • 2 comments

Which project does this relate to?

Router

Describe the bug

The search option is throwing a type error when trying to setup linkOptions props. While the code navigates properly, the type is

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-4gmm6ikn?file=src%2Froutes%2Fabout.tsx

Steps to Reproduce the Bug or Issue

Go to About page in code, Hover type error on

<HeadingLink
  title="Three"
  linkOptions={{
    to: '/posts',
    search: (prev) => ({ ...prev, tag: 'three' }),
  }}
/>
```



### Expected behavior

I expect the search options to work with typesafety as 'to' 'from' etc do

### Screenshots or Videos

_No response_

### Platform

- Router / Start Version: 1.132.47
- OS: MacOS
- Browser: Chrome
- Bundler: vite
- Bundler Version: 7.1.7


### Additional context

_No response_

alex-delia avatar Oct 09 '25 01:10 alex-delia

Solved by adding TFrom into the generic, allowing the type to properly infer when using from in the linkOptions

function FilterButton<
  TRouter extends RegisteredRouter = RegisteredRouter,
  TOptions = unknown,
  TFrom extends string = string,
>({
  label,
  isActive,
  disabled,
  linkOptions,
}: {
  label: string;
  disabled?: boolean;
  linkOptions: ValidateLinkOptions<TRouter, TOptions, TFrom>;
}) {
  return (
      <Button
        disabled={disabled}
        asChild
      >
        <Link {...linkOptions}>
          {label}
        </Link>
      </Button>
    );
  }

Should this be added to the docs or is it a bug?

alex-delia avatar Oct 09 '25 04:10 alex-delia

Had this issue today as well. Either using TFrom or LinkOptions (and adding TTo and TFrom) instead of ValidateLinkOptions fixes the issue, but it would be better if a search callback was more loose rather than failing to function at all if it cannot be narrowed

ViewableGravy avatar Nov 25 '25 02:11 ViewableGravy