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

Typescript issue when navigating without passing the to #2797

Open
fernion opened this issue Nov 18, 2024 · 0 comments
Open

Typescript issue when navigating without passing the to #2797

fernion opened this issue Nov 18, 2024 · 0 comments

Comments

@fernion
Copy link

fernion commented Nov 18, 2024

Which project does this relate to?

Router

Describe the bug

Considering a route like:
/users/$userId/details
/users/$userId/posts
/dummy/$dummyId

If we have a component that is used in the users routes that knows how to navigate to a different user, keeping the current location (details or posts),
then we can use the navigate returned from useNavigate to do so. It was suggested not to provide a to to have this working properly, and it works but Typescript complaints:

  • with navigate({ params: { userId: value }}); it complains that Object literal may only specify known properties, and userId does not exist in type
  • with navigate({ params: (prev) => ({...prev, userId: value })}); it complains that the { userId: string } is not assignable to type never

Using the from or the to, parameters solves the typescript issue, but we need to use a specific route otherwise we lose the current location,

One possibility is to use, but it might be overcomplicating the solution.

const matches = useMatches();
const to = matches[matches.length - 1].fullPath;

navigate({to, params: { userId: value });

This issue is a followup of the discussion on Discord

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-xlb7ip?file=app%2Fcomponents%2FNextUserNavigator.tsx

Steps to Reproduce the Bug or Issue

Use navigate to change only one parameter keeping any sub-route.

Expected behavior

A cleaner way to change only the parameter without TS complaints.

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: Chrome
  • Version: 130

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant