-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use typing to force parsing all query parameters #72
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.
}); | ||
const convertSearchParams = <T>( | ||
q: URLSearchParams, | ||
conversions: Required<{ [Key in keyof T]: (key: string | null) => T[Key] }> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
sort: (q.get(SORT) || undefined) as UtterancesSortableColumn | undefined, | ||
descending: q.get(DESCENDING) !== null || undefined, | ||
}); | ||
const convertStringArray = <T extends string>(s: string | null) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The T
is to seamlessly support string literals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems very smart. Typescript rules.
Description:
DEFAULT_SPACING
constant 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-files
at the end.Run
cd webapp && yarn types
while the back-end is running.our users.
README
files and our wiki for any big design decisions, if relevant.