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 · 2 comments
Open

Typescript issue when navigating without passing the to #2797

fernion opened this issue Nov 18, 2024 · 2 comments
Labels
types Changes to the typescript types

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

@SeanCassiere
Copy link
Member

SeanCassiere commented Nov 26, 2024

Edit: Ignore everything in this comment because, upon retesting with the latest versions of the packages, the runtime behaviour mentioned here has been resolved. See sandbox - https://stackblitz.com/edit/tanstack-router-gqwptx?file=app%2Fcomponents%2FNextUserNavigator.tsx


Just so I'm clear on the issue at hand here.

In this configuration, the type error goes away when setting the from value to /users/$userId/details during the navigate call, but it exits the sub-route as a whole.

Image

Whereas, when I used this modified configuration of having the from value be added onto useNavigate hook, it fixes the navigation whilst giving you the correct types.

Image

TLDR; So what we are looking to fix here is only the runtime behaviour right?


Also, for clarity on the PR, this also works.

Image

@fernion
Copy link
Author

fernion commented Nov 26, 2024

Just to clarify that the problem with the solution of not passing a to or a from, it is not a runtime issue, but a TypeScript one. On your example we can see:
Image

The TS error is:

Object literal may only specify known properties, and 'userId' does not exist in type 'ParamsReducerFn<Router<RootRoute<undefined, {}, AnyContext, AnyContext, {}, undefined, RootRouteChildren, FileRouteTypes>, TrailingSlashOption, boolean, Record<...>, Record<...>>, "PATH", string, string | undefined>'.(2353)

link.d.ts(65, 5): The expected type comes from property 'params' which is declared here on type 'NavigateOptions<Router<RootRoute<undefined, {}, AnyContext, AnyContext, {}, undefined, RootRouteChildren, FileRouteTypes>, TrailingSlashOption, boolean, Record<...>, Record<...>>, string, string | undefined, string, "">'

(property) userId: string

@SeanCassiere SeanCassiere added the types Changes to the typescript types label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Changes to the typescript types
Projects
None yet
Development

No branches or pull requests

2 participants