I'm using yup to validate a form, and I encountered this type error while trying to make this catch work:
Catch clause variable type annotation must be any or unknown if specified 1196
My Code:
const handleSubmit = async (): Promise<void> => {
try {
const isValid = await userSchema.validate(values, { abortEarly: false });
console.log(isValid);
} catch (err: ValidationError) {
console.log(err);
const errors = getValidationErrors(err);
}
getValidationErrors function:
export function getValidationErrors(err: yup.ValidationError): Errors {
console.error(err);
const validationErrors: Errors = {};
err.inner.forEach((error) => {
if (error.path) validationErrors[error.path] = error.message;
});
return validationErrors;
}
While searching about it, I found out that Typescript doesn't accept types for clause catch arguments... Why is that? This is just so commom in Java or other languages... I mean like... my solution here is attribute err: any... But isn't type any just something to never be used?