I'm using yup to invalidate the forms, when they are invalidated I want this function to return the errors, but it always gives an error in validationErrors[error.path], it gets Type 'undefined' cannot be used as an index type
import { ValidationError } from 'yup';
interface Errors {
[key: string]: string;
}
export default function getValidationErrors(err: ValidationError): Errors {
const validationErrors: Errors = {};
err.inner.forEach(error => {
validationErrors[error.path] = error.message;
});
return validationErrors;
}
----------
Console error:
Type 'undefined' cannot be used as an index type. TS2538
11 | err.inner.forEach((error) => {
> 12 | validationErrors[error.path] = error.message;
| ^
13 | });
14 |
15 | return validationErrors;