TanStack / TanStack/router

Params parse/stringify behavior doesn't match types when narrowing params

Open
#6,310 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Router

Describe the bug

Hello 👋

If I use params.parse to narrow valid param value (my exact use case is language in URL and narrowing valid values only to "en" and "pl", my use case also involves parsing them into "en-US" and "pl-PL" but I narrowed the issue down) these are narrowed both in stringify function and everywhere where I can provide a value for it (e.g. Link or redirect or navigate).

Image

However, the reality is that stringify function (where types says that it's only called with one of narrowed values) is also called with invalid values when they're visited. You can see, even TypeScript complaining that it never happens.

Image

Is this expected behavior? I tried to look into the code, but I couldn't fully grasp why stringify is called if params were never parsed in the first place. Is notFound allowed to be called like this in params.parse?

While debugging I also spotted one more thing - stringify behaves weirdly if error is thrown inside. Blank page is rendered and no log to be found anywhere. I stumbled upon it cause I used Effect schema, which threw error in stringily when I tried to decode "en-US" or "pl-PL" into "en"/"pl", because it was actually getting different value sometimes.

Your Example Website or App

https://github.com/pawelblaszczyk5/tanstack-router-not-found-params-problem

Steps to Reproduce the Bug or Issue
  1. Create params.parse function in route with path param that narrows string value into set of allowed values, otherwise throws notFound (or redirect - it also works in that case)
  2. Create params.stringify where types say it'll only ever be called with one of narrowed values
  3. Observe it being called also with values that weren't parsed correctly
Expected behavior

Types match the runtime behavior. Probably stringify is never called with value that wasn't properly parsed?

Screenshots or Videos

Provided them above in description

Platform
  • Router / Start Version: 1.145.7
  • OS: macOS
  • Browser: Reproducible everywhere
  • Browser Version: Same
  • Bundler: Vite
  • Bundler Version: 7.2.7
Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the router's params.parse and params.stringify handling, then reproduce the behavior in the linked example application using narrowed values and notFound or redirect. Verify the runtime values passed to stringify against the declared types, including the behavior when stringify throws; done means the observed runtime behavior and types agree and failures are visible.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.