interface FormikInstance {
touched: {[key: string]: boolean | undefined}
errors: {[key: string]: string | undefined}
status?: {[key: string]: string}
}
const useFormikErrors = (formik: FormikInstance) => {
const showErrors = (fieldName: string): boolean => {
const status = formik.status ? formik.status[fieldName] : undefined;
return !!formik.touched[fieldName] && (!!formik.errors[fieldName] || !!status);
}
const getErrors = (fieldName: string): string => {
const status = formik.status ? formik.status[fieldName] : undefined;
// errors is of type: string | undefined, but should be string
let errors = formik.errors[fieldName] === undefined ? '' : formik.errors[fieldName];
errors += status === undefined ? '' : status;
return errors;
}
return [showErrors, getErrors]
};
Проблема отмечена в комментарии. Переменная ошибок string | undefined
. В машинописи рассматриваются троичные операторы или я что-то упускаю здесь очевидное? Заранее спасибо.