- Notifications
You must be signed in to change notification settings - Fork928
refactor: strong type for getFormHelpers name#1029
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
codecovbot commentedApr 15, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report
@@ Coverage Diff @@## main #1029 +/- ##==========================================- Coverage 67.54% 67.32% -0.23%========================================== Files 259 259 Lines 15059 15061 +2 Branches 151 152 +1 ==========================================- Hits 10172 10140 -32- Misses 3873 3899 +26- Partials 1014 1022 +8
Continue to review full report at Codecov.
|
// getIn is a util function from Formik that gets at any depth of nesting, and is necessary for the types to work | ||
export const getFormHelpers = <T>(form: FormikContextType<T>, name: keyof T, error?: string): FormHelpers => { | ||
if (typeof name !== "string") { | ||
throw new Error(`name must be type of string, instead received '${typeof name}'`) |
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.
What happens if somehow this gets thrown in production?
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 a desirable case at this stage. We don't yet have an error boundary ported (#1013 ), but you'd hit that.
Uh oh!
There was an error while loading.Please reload this page.
Summary:
Modify
getFormHelpers
so that only the keys of the generic form interfacecan be passed in for
name
.Details:
Keys of an object in TS can naturally be of
string | number | symbol
. I don't thinkthere's an easy way to re-genericize this helper and type things that gets around this,
so I took the simple path of throwing an
Error
ontypeof !== "string"
.Impact:
Minor improvement to dev experience using
getFormHelpers
(compile-time safety).