Skip to content

Fix TypeError when optional params omitted from array (fixes #736)#878

Merged
bakerkretzmar merged 1 commit intotighten:2.xfrom
pataar:fix/optional-params-array-types
Mar 6, 2026
Merged

Fix TypeError when optional params omitted from array (fixes #736)#878
bakerkretzmar merged 1 commit intotighten:2.xfrom
pataar:fix/optional-params-array-types

Conversation

@pataar
Copy link
Contributor

@pataar pataar commented Mar 6, 2026

Closes #736

Summary

When passing route parameters as an array, omitting optional parameters caused a TypeScript error — even though the parameters were optional. This PR fixes that by making optional parameters truly optional in the generated tuple type.

Before

Given a route like:

interface RouteList {
    'posts.comments.show': [
        { name: 'post'; required: true },
        { name: 'comment'; required: false },
    ];
}

These perfectly valid calls would all cause type errors:

route('posts.comments.show', [2]);        // ❌ Source has 1 element(s) but target requires 2
route('posts.comments.show', ['foo']);     // ❌
route('posts.comments.show', [{ id: 2 }]); // ❌

The workaround was to always pass all parameters or use object syntax instead.

After

All of the above now work as expected — no type errors, no workarounds needed:

route('posts.comments.show', [2]);          // ✅
route('posts.comments.show', ['foo']);       // ✅
route('posts.comments.show', [{ id: 2 }]);  // ✅
route('posts.comments.show', [2, 3]);       // ✅ (still works too)

How it works

The old KnownRouteParamsArray type mapped all parameters to required tuple positions regardless of whether they were optional. The fix splits it into two recursive helper types:

  • RequiredParamsTuple — collects required params as required tuple elements
  • OptionalParamsTuple — collects optional params as optional tuple elements (?), with ...unknown[] at the end for extra items

So for the route above, the generated type goes from [ParameterValue, ParameterValue, ...unknown[]] to [ParameterValue, ParameterValue?, ...unknown[]].

Test plan

  • Uncommented 4 existing TODO test cases that were previously blocked by this bug
  • Added new tests for routes where all parameters are optional (route('optional', []) and route('optional', ['foo']))
  • All JS and type tests pass

Split KnownRouteParamsArray into RequiredParamsTuple and
OptionalParamsTuple so that optional route parameters become optional
tuple elements. This allows passing fewer array items when trailing
parameters are optional, e.g. `route('posts.comments.show', [2])`.

Fixes tighten#736
@bakerkretzmar
Copy link
Collaborator

Wow........ this is awesome. Thank you so much!

@bakerkretzmar bakerkretzmar merged commit 309e69b into tighten:2.x Mar 6, 2026
26 checks passed
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

Successfully merging this pull request may close these issues.

TypeError when optional params omitted from array

2 participants