Redux Saga :类型'string'的参数无法指派给类型'TakeableChannel '的参数< unknown>

pieyvz9o  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(178)

使用有效负载调度操作,会产生以下类型脚本错误:

Argument of type 'string' is not assignable to parameter of type 'TakeableChannel'.

示例:

export default function* watchAuth() {
yield* takeLatest(startAuth.toString(), handleAuthUser); // Argument of type 'string' is not assignable to parameter of type 'TakeableChannel'.
}

使用:

"@reduxjs/toolkit": "^1.8.5",
"typed-redux-saga": "^1.5.0",

编辑:handleAuthUser生成器函数

function* handleAuthUser({ payload: { fields, isRegister } }) {
  const { email, password } = fields || {};
  try {
    if (isRegister) {
      // User registering an account
      yield* call(registerAWS, fields);
      yield* put(promptConfirmation({ email, password }));
    } else {
      // User is logging into his account
      const cognitoUser = yield* call(newCognitoUser, email);

      const authDetails = new AuthenticationDetails({
        Username: email,
        Password: password,
      });

      const res = yield* call(loginAWS, cognitoUser, authDetails);
      if (res.userAttributes) {
        const { email_verified, phone_number_verified, ...userAttributes } = res.userAttributes;
        yield* put(
          promptNewPassword({
            email,
            userAttributes,
            cognitoUser,
          }),
        );
      } else {
        yield* put(checkAuth());
      }
    }
  } catch (error) {
    switch ((error as any).code) {
      // switch cases
    }
  }
}

异径管:

startAuth: (
      state,
      action: PayloadAction<{ fields: { [key: string]: string }; isRegister?: boolean }>,
    ) => {
      state.loading = true;
      state.errors = { login: {}, signup: {}, other: {} };
      state.userPendingConfirmation = null;
      state.userPendingNewPassword = null;
      state.resetPasswordUser = null;
    },
jdg4fx2g

jdg4fx2g1#

要解决这个问题,你需要修改handleAuthUser的类型。问题是takeLatest期望要运行的 Saga 的第一个参数(handleAuthUser)是一个带有type属性的对象,但是你现在定义它的方式typescript假设payload是该对象的唯一属性,这与期望不符。
因此将其更改为:

type AnyAction = {type: string, [key: string]: any}
function* handleAuthUser({ payload: { fields, isRegister } }: AnyAction) {

将修复它,因为您告诉TS该对象在其他操作参数中还具有type属性。

相关问题