Skip to content

URL-Params are kept even if not provided requiring TypeScript error when having exactOptionalPropertyTypes enabled #5582

@SimonSimCity

Description

@SimonSimCity

Which project does this relate to?

Router

Describe the bug

Since required parameters are also required on the params-prop of the Link component, I would expect those not being present to be removed.

In the stackblitz example, I defined a route as /users/{-$id} and added some links to it:

  1. Menu - without params prop.
  2. Link "User1" - having params prop setting id to 1.
  3. Link "User2" - having params prop setting id to 2.
  4. Link "List (not working 😬)" - having params prop with an empty object.
  5. Link "List (working 🤩)" - having params prop setting id to undefined.

The last option is invalid when having exactOptionalPropertyTypes enabled in your TypeScript config.

When imagining a different scenario, where I have multiple variables, some of them required, I can actually (against the types) leave out parameters already provided by the former route. In my real-world scenario I try to build a link to and from /company/$companyId/users/{-$userId}. Based on the types I have to provide companyId, but based on the implementation, I don't have to, but I should set userId to undefined if I want to remove it, which creates a TypeScript error, because I have exactOptionalPropertyTypes enabled.

Your Example Website or App

https://stackblitz.com/edit/github-abbyhecx?file=src%2Froutes%2Fusers%2F{-%24id}.tsx,src%2Froutes%2F__root.tsx

Steps to Reproduce the Bug or Issue

  1. Go to "Users".
  2. Click on "User1" to set the optional parameter.
  3. Try to get back to the "Users" page by clicking in the menu.

Expected behavior

The "List (not working 😬)" should work, means, parameters, not set when providing params should be omitted.

Screenshots or Videos

No response

Platform

  • Router Version: 1.133.22
  • OS: macOS
  • Browser: Chrome
  • Browser Version: 140
  • Bundler: vite
  • Bundler Version: 7.1.7

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions