Moh*_*nir 5 typescript redux redux-saga redux-toolkit
使用有效负载调度操作会产生以下打字稿错误:
Argument of type 'string' is not assignable to parameter of type 'TakeableChannel'.
Run Code Online (Sandbox Code Playgroud)
例子:
export default function* watchAuth() {
yield* takeLatest(startAuth.toString(), handleAuthUser); // Argument of type 'string' is not assignable to parameter of type 'TakeableChannel'.
}
Run Code Online (Sandbox Code Playgroud)
使用:
"@reduxjs/toolkit": "^1.8.5",
"typed-redux-saga": "^1.5.0",
Run Code Online (Sandbox Code Playgroud)
编辑: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
}
}
}
Run Code Online (Sandbox Code Playgroud)
减速器:
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;
},
Run Code Online (Sandbox Code Playgroud)
要解决这个问题,您需要修改handleAuthUser. 问题是,takeLatest期望将要运行的传奇的第一个参数(handleAuthUser)是一个具有type属性的对象,但现在定义它的方式打字稿假定payload是该对象的唯一属性,这并不符合预期。
所以将其更改为:
type AnyAction = {type: string, [key: string]: any}
function* handleAuthUser({ payload: { fields, isRegister } }: AnyAction) {
Run Code Online (Sandbox Code Playgroud)
将修复它,因为您告诉 TS 该对象在其他操作参数中还具有属性type。
| 归档时间: |
|
| 查看次数: |
2131 次 |
| 最近记录: |