Skip to content
Source
Sign inSign up
TanStack/router

Functional Search Setter causes type error when using ValidateLinkOptions #3683

Send to my agent
Openseththoburn-cc(GitHub profile, opens in a new tab) opened this issue 5 comments

Which project does this relate to?

Router

Describe the bug

I made a link component using ValidateLinkOptions as explained in the docs. The component works fine if I pass search to link options as an object, but if search is a setter function, I get: Type '{ test: string; }' is not assignable to type 'never'.

Your Example Website or App

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

Steps to Reproduce the Bug or Issue

Open the link and navigate to src/routes/about.tsx. Notice the two heading components, the one with the functional search setter has a type error.

Expected behavior

There should be no type error

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome

Additional context

No response

  1. Still haven't found the cause exactly, but if anyone is looking for a workaround, explicitly annotating the prev type is typesafe and fixes the issue.

    <Button
      linkProps={{
        from: '/items',
        search: (prev: ItemSearch) => ({
          ...prev,
          editItem: 'new', 
        }),
      }}
      text="New Item"
    />
  2. I'm seeing the same error when using the functional search setter in navigate, without any validateSearch options: https://stackblitz.com/edit/github-f2x2vmdq?file=src%2Froutes%2F__root.tsx,src%2Froutes%2Findex.tsx

  3. @bhainesva that's not the same thing. you need to either set from or to

  4. Not sure if this is the same issue. But I'm seeing type narrowing on the search args doesn't seem to be adhering to the from argument. It's just showing a union of all search params. Here's an example:

    Types just never seem to be happy when using the arrow function.

    https://stackblitz.com/edit/tanstack-router-l8hnhddv?file=src%2Froutes%2Ftest-1234.tsx

  5. woops mixed this up with something else. This is still open. ValidateLinkOptions works for object search values, but it does not reliably preserve the source search schema for functional setters.

    That means search: (prev) => ({ ...prev, editItem: 'new' }) can either fail with never or expose a union of unrelated search schemas. The same helper is shared by the React, Solid, and Vue adapters.

    We probably need type coverage for the documented generic wrapper pattern before changing the inference logic, including functional setters with an explicit from.

Sign in to join this conversation.