I'm struggling to access the error response data. The code worked fine in JavaScript, but since moving to TypeScript, things have gotten confusing. Currently, I'm seeing 'error' is of type 'unknown'.ts(18046)
export const registerClient = createAsyncThunk('clients/register', async (userData: object, { rejectWithValue }) => {
try {
const response = await axiosInstance.post('/web/client/register', userData);
return response.data;
} catch (error) {
return rejectWithValue(error.response.data.error);
}
});
Things I've tried
- Getting an Unexpected 'any' error, can't use any typereturn rejectWithValue((error as any).response.data.error);
- Tried creating a new interface below code - received this error,
error' is of type 'unknown'
interface ErrorResponse {
error: string;
}
const errorResponse: ErrorResponse = error.response.data;
return rejectWithValue(errorResponse.error);
Does anyone have a proper solution for this? Thank you very much