type BackendErrorResponseType = | { [key in TField]: string } | string type CallbackArgType = { field: TField description: string } type CallbackType = (arg: CallbackArgType) => void export const handleFormError = ( e: BackendErrorResponseType, callback: CallbackType ) => { if (typeof e === 'string') { callback({ field: '' as TField, description: e }) return } Object.entries(e).forEach(([key, value]) => { callback({ field: key as TField, description: value as string }) }) }