Fix TypeError when optional params omitted from array (fixes #736)#878
Merged
bakerkretzmar merged 1 commit intotighten:2.xfrom Mar 6, 2026
Merged
Conversation
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
Collaborator
|
Wow........ this is awesome. Thank you so much! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
These perfectly valid calls would all cause type errors:
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:
How it works
The old
KnownRouteParamsArraytype 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 elementsOptionalParamsTuple— collects optional params as optional tuple elements (?), with...unknown[]at the end for extra itemsSo for the route above, the generated type goes from
[ParameterValue, ParameterValue, ...unknown[]]to[ParameterValue, ParameterValue?, ...unknown[]].Test plan
route('optional', [])androute('optional', ['foo']))