Answer the question
In order to leave comments, you need to log in
How to fix "TS2769: No overload matches this call." when dispatching an asynchronous action?
I just can’t understand what the problem is, ThunkAction seems to be correctly typed. When dispatching the authorization check in index.js, an error pops up:
store.dispatch(UserOperation.checkAuth()); // Тут ошибка
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root"),
);
export const rootReducer = combineReducers({
// DATA: data,
// APP: app,
USER: user,
});
export type GlobalState = ReturnType<typeof rootReducer>;
export type InferActionsTypes<T> = T extends { [key: string]: infer U }
? U
: never;
export type BaseThunkActionType<A extends Action = Action> = ThunkAction<
Promise<void>,
GlobalState,
AxiosInstance,
A
>;
type UserActionTypes = ReturnType<InferActionsTypes<typeof ActionCreator>>;
type ThunkActionType = BaseThunkActionType<UserActionTypes>;
export const initialState = {
authorizationStatus: false as boolean,
user: {
id: -1,
email: ``,
isPro: false,
avatar: ``,
name: ``,
} as UserLogged,
isAuthorizationLoading: true as boolean,
};
type InitialStateType = typeof initialState;
export const ActionType = {
SET_AUTHORIZATION_STATUS: `SET_AUTHORIZATION_STATUS`,
GET_USER_DATA: `GET_USER_DATA`,
FINISH_AUTHORIZATION: `FINISH_AUTHORIZATION`,
} as const;
export const ActionCreator = {
finishAuthorizationLoading: () => {
return {
type: ActionType.FINISH_AUTHORIZATION,
payload: false,
};
},
setAuthorizationStatus: (status: boolean) => {
return {
type: ActionType.SET_AUTHORIZATION_STATUS,
payload: status,
};
},
getUserData: (userData: UserLogged) => {
return {
type: ActionType.GET_USER_DATA,
payload: userData,
};
},
};
export const Operation = {
checkAuth: (): ThunkActionType => async (
dispatch,
getState,
api,
): Promise<void> => {
try {
const response = await api.get(`/login`);
dispatch(ActionCreator.setAuthorizationStatus(true));
dispatch(ActionCreator.getUserData(createUser(response.data)));
dispatch(ActionCreator.finishAuthorizationLoading());
} catch (e) {
dispatch(ActionCreator.setAuthorizationStatus(false));
dispatch(ActionCreator.finishAuthorizationLoading());
}
},
login: (authData: LoginData): ThunkActionType => async (
dispatch,
getState,
api,
): Promise<void> => {
const response = await api.post(`/login`, {
email: authData.email,
password: authData.password,
});
dispatch(ActionCreator.setAuthorizationStatus(true));
dispatch(ActionCreator.getUserData(createUser(response.data)));
history.push(`/`);
},
};
export const reducer = (
state = initialState,
action: UserActionTypes,
): InitialStateType => {
switch (action.type) {
case ActionType.SET_AUTHORIZATION_STATUS:
return { ...state, authorizationStatus: action.payload };
case ActionType.GET_USER_DATA:
return { ...state, user: action.payload };
case ActionType.FINISH_AUTHORIZATION:
return { ...state, isAuthorizationLoading: action.payload };
default:
return state;
}
};
Answer the question
In order to leave comments, you need to log in
Seems to have figured it out. It was necessary to set the correct types in the applyMiddleware call (I didn’t do this at all).
Here is an example:
const store = createStore(
rootReducer,
composeWithDevTools(
applyMiddleware(
thunk.withExtraArgument(api) as ThunkMiddleware<
GlobalState,
AllReduxActions,
AxiosInstance
>,
),
),
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question