I'm coding a functional component in redux and I added a return values for a specific action.
The new value comes from a Promise so if the type is called Ival the return value will be of type Promise<Ival | null> (null in case the response failed).
My question is how do I determine the return type to Ival?
This is the action:
export const addVal = (data: IData) => async (dispatch: IDispatch): Promise<IVal | null> => {
try {
const resp = await api.vals.create(data);
dispatch({ type: "CREATE_VAL", payload: resp });
return resp;
} catch (error) {
dispatch({ type: "ERROR_MSG" ,payload: error });
return null;
}
};
this is the function I'm using the dispatch:
const createNewConnection = async (data: IData): Promise<void> => {
try {
const newVal: IVal | null = await dispatch(addVal(data));
onValCreated(newVal);
} catch (e) {
onValCreated(null);
}
};
The onValCreated function receives an IVal type as argument and this is the Error message:
"TS2559: Type '(dispatch: IDispatch) => Promise ' has no properties in common with type 'IVal'"
I tried on dispatch both:
// First try
const newVal: IVal | null = await dispatch<IVal | null>(addVal(data));
// Second try
const newVal: IVal | null = await dispatch<Promise<IVal | null>>(addVal(data));
But it didn't work