You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone! I have a question about the best practice for handling programmatic, server-side errors.
The Goal:
I want to display a server error from a useMutation (tanstack) onError callback on the form. The most direct way seems to be form.setErrorMap.
The Problem & The Workaround:
When in onError I call form.setErrorMap({ onSubmit: { form: [err.message], fields: {} } }), I've noticed that TypeScript can't correctly infer the type for state.errorMap.onSubmit unless I explicitly add a placeholder validator.
...throws the error Property 'map' does not exist on type 'never'.
I understand this happens because the errorMap type is inferred entirely from the validators object provided to useForm. If the onSubmit key isn't in validators, TypeScript correctly assumes state.errorMap.onSubmit doesn't exist.
The working solution is to add a placeholder validator, like this:
const form = useForm({
// ...
validators: {
onChange: zodValidator(MySchema),
// This placeholder is added only to satisfy TypeScript
onSubmit: () => [],
},
});
My question is: Is this the intended, idiomatic way to handle this scenario or am I missing something?
If so, maybe it may be improved somehow, maybe the type of setErrorMap be enhanced to allow setting errors for keys not present in validators, or maybe somthing like root. in RHF?
It its not possible at all - sorry, but I've just glanced at the source code.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone! I have a question about the best practice for handling programmatic, server-side errors.
The Goal:
I want to display a server error from a
useMutation
(tanstack) onError callback on the form. The most direct way seems to beform.setErrorMap
.The Problem & The Workaround:
When in onError I call
form.setErrorMap({ onSubmit: { form: [err.message], fields: {} } })
, I've noticed that TypeScript can't correctly infer the type forstate.errorMap.onSubmit
unless I explicitly add a placeholder validator.Without the placeholder, this code:
...throws the error
Property 'map' does not exist on type 'never'.
I understand this happens because the
errorMap
type is inferred entirely from thevalidators
object provided touseForm
. If theonSubmit
key isn't in validators, TypeScript correctly assumesstate.errorMap.onSubmit
doesn't exist.The working solution is to add a placeholder validator, like this:
My question is: Is this the intended, idiomatic way to handle this scenario or am I missing something?
If so, maybe it may be improved somehow, maybe the type of
setErrorMap
be enhanced to allow setting errors for keys not present in validators, or maybe somthing likeroot
. in RHF?It its not possible at all - sorry, but I've just glanced at the source code.
Beta Was this translation helpful? Give feedback.
All reactions