feat(matchedData): add generic types for return #1246
Merged
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.
Description
As brought up in #1041, it's currently hard to type the result of
parsedData
. It comes with all fields set asany
unless we do a type assertion via theas
keyword.However, this syntax overrides all type-checking. To mitigate that, this PR adds an optional generic type to the
parsedData
function signature, so it becomes:Allowing users to keep using
matchedData
with the same signature as before:but to provide a type signature of the expected return (if it's been validated) if they'd like:
There are no breaking changes, since
matchedData
still keepsRecord<string, any>
as the default return type.Note
While types might not be strictly accurate depending on how the generic type is created, I believe it's better than the current solution (
Record<string, any>
).As suggested in #1041 (comment), though, I believe a more type could be provided from the library depending on the options provided (e.g. with
includeOptionals: true
) via type guards. I'm open to exploring that direction further if you (maintainers) believe it might be worth implementing 馃憤To-do list