Use typing to force parsing all query parameters#72
Merged
JosephMarinier merged 2 commits intodevfrom May 18, 2022
Merged
Conversation
as we were overwriting with the default value. This used to make sense as we were reusing the `DEFAULT_SPACING` constant elsewhere, but we no longer do.
JosephMarinier
commented
May 17, 2022
| }); | ||
| const convertSearchParams = <T>( | ||
| q: URLSearchParams, | ||
| conversions: Required<{ [Key in keyof T]: (key: string | null) => T[Key] }> |
Contributor
Author
There was a problem hiding this comment.
That's the important bit: The Required ensures that a conversion function is defined for all query parameters (all keyof T) defined in the different Query...State types, even if they have a ?:. This ensures that q.get() gets called with only parameter names from a Query...State type. This replaces the 12 calls to q.get() with 12 string constants, which seemed error-prone.
JosephMarinier
commented
May 17, 2022
| sort: (q.get(SORT) || undefined) as UtterancesSortableColumn | undefined, | ||
| descending: q.get(DESCENDING) !== null || undefined, | ||
| }); | ||
| const convertStringArray = <T extends string>(s: string | null) => |
Contributor
Author
There was a problem hiding this comment.
The T is to seamlessly support string literals.
christyler3030
approved these changes
May 18, 2022
christyler3030
left a comment
There was a problem hiding this comment.
This seems very smart. Typescript rules.
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.
Description:
DEFAULT_SPACINGconstant elsewhere, but we no longer do.Checklist:
You should check all boxes before the PR is ready. If a box does not apply, check it to acknowledge
it.
ran
pre-commit run --all-filesat the end.Run
cd webapp && yarn typeswhile the back-end is running.our users.
READMEfiles and our wiki for any big design decisions, if relevant.