使用有效負載調度操作,會產生此打字稿錯誤:
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;
},
uj5u.com熱心網友回復:
要解決此問題,您需要修改handleAuthUser. 問題是takeLatest期望將要運行的傳奇的第一個引數(handleAuthUser)是具有type屬性的物件,但是您現在定義它的方式打字稿假定payload是該物件的唯一屬性,它沒有符合預期。
因此將其更改為:
type AnyAction = {type: string, [key: string]: any}
function* handleAuthUser({ payload: { fields, isRegister } }: AnyAction) {
將修復它,因為您告訴 TS 該物件在其他操作引數中還具有該type屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529954.html
上一篇:反應打字稿可選引數
