Skip to content
Source
Sign inSign up
TanStack/router

fix: type error when passing number to `Link.params` #5560

Openscarf005(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/5560/headopened

Showing 1 changed file with 10 additions and 1 deletion.

x
0 of 1 viewed, 1 left
10 additions and 1 deletionpackages/router-core/src/link.tsModified
View file
@@ -574,7 +574,16 @@
in out TFrom,
in out TTo,
> {
- params: MakeRequiredParamsReducer<TRouter, 'PATH', TFrom, TTo> & {}
+ params: MakeRequiredParamsReducer<
+ TRouter,
+ 'PATH',
+ TFrom,
+ TTo
+ > extends infer Params
+ ? {
+ [K in keyof Params]: Params[K] | number
+ } & {}
+ : never
Thread on lines 577 to 586

⚠️ Potential issue | 🟡 Minor

Apply the same number allowance to optional path params.

The change allows numbers for required path params, but MakeOptionalPathParams (lines 547-553) doesn't have the same allowance. If numbers are auto-stringified at runtime per the documentation, this behavior should be consistent for both required and optional path parameters.

After fixing the type logic issue above, apply similar logic to MakeOptionalPathParams:

export interface MakeOptionalPathParams<
  in out TRouter extends AnyRouter,
  in out TFrom,
  in out TTo,
> {
  params?: true | (ParamsReducer<TRouter, 'PATH', TFrom, TTo> extends infer Params
    ? Params extends true
      ? true
      : Params extends (...args: any[]) => any
        ? Params
        : {
            [K in keyof Params]: Params[K] | number
          } & {}
    : never)
}
🤖 Prompt for AI Agents
In packages/router-core/src/link.ts around lines 547-553 and 577-586,
MakeOptionalPathParams currently doesn't allow numeric path param values while
MakeRequiredParamsReducer does; update MakeOptionalPathParams so its mapped type
also widens each param type to Params[K] | number (i.e., when resolving Params
into an object map, transform each key's type to allow number in addition to the
original type) and keep the existing true/functional branches intact so optional
params behave consistently with required params.

⚠️ Potential issue | 🟠 Major

Fix type logic to handle true and function cases.

The mapped type { [K in keyof Params]: Params[K] | number } assumes Params is an object with parameter keys, but MakeRequiredParamsReducer can also resolve to:

  • true (when params don't need to change between routes)
  • A function of type ParamsReducerFn

When Params is true, keyof true evaluates to never, causing the mapped type to become {} and lose the intended true semantic. When Params is a function, keyof Function includes properties like "length", "name", "call", etc., creating an incorrect type.

Consider this approach that explicitly handles all three cases:

-  params: MakeRequiredParamsReducer<
-    TRouter,
-    'PATH',
-    TFrom,
-    TTo
-  > extends infer Params
-    ? {
-        [K in keyof Params]: Params[K] | number
-      } & {}
-    : never
+  params: MakeRequiredParamsReducer<TRouter, 'PATH', TFrom, TTo> extends infer Params
+    ? Params extends true
+      ? true
+      : Params extends (...args: any[]) => any
+        ? Params
+        : {
+            [K in keyof Params]: Params[K] | number
+          } & {}
+    : never
📝 Committable suggestion

‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

  params: MakeRequiredParamsReducer<TRouter, 'PATH', TFrom, TTo> extends infer Params
    ? Params extends true
      ? true
      : Params extends (...args: any[]) => any
        ? Params
        : {
            [K in keyof Params]: Params[K] | number
          } & {}
    : never
🤖 Prompt for AI Agents
In packages/router-core/src/link.ts around lines 577 to 586, the current mapped
type assumes Params is an object and uses {[K in keyof Params]: Params[K] |
number} which breaks when MakeRequiredParamsReducer resolves to true (loses the
true semantic) or to a function (pulls in function keys); change the type to a
conditional that explicitly handles all three cases: if Params extends true then
keep true, else if Params extends the ParamsReducerFn type then keep Params (or
Params | number if numbers are valid for reducer functions per usage), else
treat Params as an object and map its keys to Params[K] | number; remove the
fallback to an empty intersection (& {}) and ensure the function case is
excluded from key remapping by using the conditional branches.
​
export interface MakeRequiredSearchParams<