alt approach to knowing what params are optional #464
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.
The Background
Currently, anything that can have params is normalized to
WithParams
type. This type has aparams
property that storesRecord<string, Param>
. The key for each param is where we store if we expect the param to be optional or not.For example:
This
WithParams
has a single param "foo", which we can see is NOT optional.Versus this
WithParams
, where "foo" is now optional.The Motivation
We've never loved sticking the leading "?" in the params object for several reasons
withParams
objects we have to de-normalize param names back to exclude the question markvalue
propertyThat last reason is one that I found the most compelling and why I chose to try this syntax. This is also something that I didn't love about the suggestion to introduce a new
optional
property that only tracks what params are optional.The Solution
Now
WithParams
leaves params alone and looks at thevalue
to determine what's optional.What About The Types?
I fully implemented this change at runtime, got tests passing, tested with router-preview, and totally expected types to be broken. Much to my surprise it seems the types already work in the way the runtime now works?