When utilizing a createAsyncThunk function, I encountered an issue with retrieving the message field of an error when handling a status 401. The error is being treated as type 'unknown'.ts(18046)
export const add = createAsyncThunk<any, Product, { state: RootState }>(
'product/addToCart',
async (product, { getState, dispatch, rejectWithValue }) => {
const response = await fetch(`${base_url}/main/wishlist/card`, {
method: 'POST',
body: JSON.stringify(product),
headers: {
Authorization: `Bearer ${getState().user.jwtToken}`,
'Content-Type': 'application/json'
}
})
try {
if (response.ok) {
const data = await response.json();
console.log("Successful addition of a product to the wishlist");
return data;
}
if (response.status === 400) {
dispatch(refreshTokenRotation({}));
dispatch(add(product));
}
if (response.status === 401) {
throw new Error("Invalid or missing jwtToken");
}
}
catch (err) {
return rejectWithValue(err.message);//'err' is of type 'unknown'.ts(18046)
}
}
)
I attempted specifying the argument type but the issue persists.
catch (error: Error) {
console.log(error);
return rejectWithValue(error.message);
}