React-Typescript:期望一个赋值或函数调用,而不是一个表达式

React-Typescript: Expected an assignment or function call and instead saw an expression

我正在使用 React 和 Redux 进行注册,我已经完成了我需要的所有操作和 reduser,但是我收到了这个错误:

Expected an assignment or function call and instead saw an expression 

这个文件是一个Action Creator,我通过它使用Dispatch与Reducer进行通信。

auth.ts:(动作创作者)

export const signupActionCreator = (
  email: string,
  name: string
) => {
  return async (dispatch: Dispatch<UserTypes>) => {
    return AuthService.signup(email, name)
      .then((res) => {
        // tttt
        localStorage.setItem('token', res.data)
        dispatch({
          type: Types.SIGNUP,
          // payload: res.data.message,
          // tttt
          payload: res.data  
        });

        return Promise.resolve();
      })
      .catch((err) => {
        console.log(err);
        return Promise.reject();
      });
  };
};

这是文件,我为每个操作使用了一个类型,并且使用了枚举。

types.ts:

export enum Types {
  SIGNUP = "SIGNUP"
}

通过这个文件,我能够与后端通信。

authServices.ts:

import API_URL from "../http-common";
import axios from "axios";

const signup = (email: string, name: string) => {
  return axios.post(API_URL + "/authentication/sign-up", { email, name });
};
const AuthService = {
  signup,
};

export default AuthService;

通过这个文件我可以定义接口。

auth.d.ts:

export interface UserData {
    name: string;
    email: string;
}

export interface UserState {
    data: UserData[];
}

export interface UserAction {
    type: string;
    payload: any;
}

export type UserTypes = UserAction;

您只需要 return 来自 signupActionCreator 的这个:

export const signupActionCreator = (email: string, name: string) => {
  return async (dispatch: Dispatch<UserAction>) => {

或者赋值给变量:

export const signupActionCreator = (email: string, name: string): void => {
  const testVariable = async (dispatch: Dispatch<UserAction>) => {

我读了这个 post 并了解了导致问题的原因:

https://www.codecademy.com/forum_questions/505a2f774f0e7400020021ba

我跟踪我的代码,发现问题出在 authServices,ts 文件中,我必须分配变量“response”然后 return 变量“response”。

async function signup(email: string, name: string) {
  const response = await axios.post(API_URL + "/authentication/sign-up", { email, name });
  return response;
};

然后我的问题就解决了