Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative navigation always expects a search param? #1838

Open
mirague opened this issue Jun 27, 2024 · 7 comments
Open

Relative navigation always expects a search param? #1838

mirague opened this issue Jun 27, 2024 · 7 comments
Labels
information needed Further information is requested

Comments

@mirague
Copy link

mirague commented Jun 27, 2024

Describe the bug

When using navigation (e.g. navigate / redirect) with a from and relative to, it seems TypeScript always complains about the search param being required, even if none of the routes define a validateSearch.

This example works at runtime, but TypeScript complains:

export const Route = createFileRoute('/posts/$postId')({
  beforeLoad: () => {
      // works, but TypeScript error about required "search" param
      throw redirect({
        from: Route.fullPath,
        to: '../',
      });
  }
});

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-ycpp7o?file=src%2Froutes%2Fposts.%24postId.tsx

Steps to Reproduce the Bug or Issue

  1. Open the posts.$postId.tsx file and go to the render function
  2. Observe there's a TypeScript error, even though the actual code works

Expected behavior

I expect that if the to route doesn't provide a validateSearch, it's not a required prop and TypeScript doesn't complain.

Screenshots or Videos

CleanShot 2024-06-27 at 11 06 30@2x

Platform

  • OS: macOS / Arc

Additional context

No response

@sepirak-nick
Copy link

Getting the same issue, upgrading from 1.29 to 1.40.

@schiller-manuel
Copy link
Contributor

The error you see is caused by the trailingSlash router property:

https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#trailingslash-property

It defaults to never, so in that case you should use

        to: '..',

see https://stackblitz.com/edit/tanstack-router-xpxvgs?file=src%2Froutes%2Fposts.%24postId.tsx,src%2Fmain.tsx

If you changetrailingSlash to either preserve or always, you can use to: '../'.

see https://stackblitz.com/edit/tanstack-router-wxombr?file=src%2Froutes%2Fposts.%24postId.tsx

@mirague
Copy link
Author

mirague commented Jun 28, 2024

@schiller-manuel Thanks a lot for pointing it out, that was not clear to me from the TS error!

Do you think the type should be tweaked, either to be more permissive (allowing trailing slashes ../ in to param), or not include ../ in autosuggestion?

Typescript does auto-suggest ../:
CleanShot 2024-06-28 at 18 58 53

@schiller-manuel
Copy link
Contributor

Let's ask @chorobin about his opinion on this

@SeanCassiere SeanCassiere added the information needed Further information is requested label Jul 4, 2024
@judgemavi
Copy link

Looks like in the latest version search param is required if route has validateSearch set.

I have zod schema with all properties set to optional but link and navigate still require search to be provided

@chorobin
Copy link
Contributor

Let's ask @chorobin about his opinion on this

Yeah, we should at least not suggest '../' and suggest '..' instead

@chorobin
Copy link
Contributor

chorobin commented Jul 18, 2024

Looks like in the latest version search param is required if route has validateSearch set.

I have zod schema with all properties set to optional but link and navigate still require search to be provided

Do you have an example where this is not working?

Generally the type logic for required/optional search params works off the basis that, if {} is assignable to your search params then it is optional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
information needed Further information is requested
6 participants