gpt4 book ai didi

reactjs - React + Redux-Observable + Typescript - 编译,参数不可分配错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:59 26 4
gpt4 key购买 nike

我正在使用 React 和 Redux-Observable 创建一个应用程序。我是新手,我正在尝试创建史诗来执行用户登录。

我的史诗如下:

export const loginUserEpic = (action$: ActionsObservable<Action>) =>
action$.pipe(
ofType<LoginAction>(LoginActionTypes.LOGIN_ACTION),
switchMap((action: LoginAction) =>
ajax({
url,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: { email: action.payload.username, password: action.payload.password },
}).pipe(
map((response: AjaxResponse) => loginSuccess(response.response.token)),
catchError((error: Error) => of(loginFailed(error))),
),
),
);

问题是我在这一行收到 Typescript 错误:ofType<LoginAction>(LoginActionTypes.LOGIN_ACTION)这么说:

Argument of type '(source: Observable<LoginAction>) => Observable<LoginAction>' is not assignable to parameter of type 'OperatorFunction<Action<any>, LoginAction>'.
Types of parameters 'source' and 'source' are incompatible.
Type 'Observable<Action<any>>' is not assignable to type 'Observable<LoginAction>'.
Type 'Action<any>' is not assignable to type 'LoginAction'.
Property 'payload' is missing in type 'Action<any>'.

我的操作在这里:

export enum LoginActionTypes {
LOGIN_ACTION = 'login',
LOGIN_SUCCESS_ACTION = 'login-sucesss',
LOGIN_FAILED_ACTION = 'login-failed',
}

export interface LoginAction extends Action {
type: LoginActionTypes.LOGIN_ACTION;
payload: {
username: string;
password: string;
};
}

export function login(username: string, password: string): LoginAction {
return {
type: LoginActionTypes.LOGIN_ACTION,
payload: { username, password },
};
}

export interface LoginSuccessAction extends Action {
type: LoginActionTypes.LOGIN_SUCCESS_ACTION;
payload: {
loginToken: string;
};
}

export function loginSuccess(loginToken: string): LoginSuccessAction {
return {
type: LoginActionTypes.LOGIN_SUCCESS_ACTION,
payload: { loginToken },
};
}

export interface LoginFailedAction extends Action {
type: LoginActionTypes.LOGIN_FAILED_ACTION;
payload: {
error: Error;
};
}

export function loginFailed(error: Error): LoginFailedAction {
return {
type: LoginActionTypes.LOGIN_FAILED_ACTION,
payload: { error },
};
}

export type LoginActions = LoginAction | LoginSuccessAction | LoginFailedAction;

如何在不使用 any 的情况下解决这个问题史诗类型?

最佳答案

ofType redux-observable 提供的运营商不是区分联合类型的最佳方法。更好的方法是使用 isOfType typesafe-actions 提供的功能.

import { filter } from 'rxjs/operators';
import { isOfType } from 'typesafe-actions';

首先,让我们告诉 TypeScript 您的应用程序中可能使用的操作。您的操作流不应定义为 ActionsObservable<Action> ,但作为您的 操作的流:ActionsObservable<LoginActions> .

export const loginUserEpic = (action$: ActionsObservable<LoginActions>) =>

现在我们可以使用 isOfType谓词连同 filter运算符(operator)。替换为:

ofType<LoginAction>(LoginActionTypes.LOGIN_ACTION)

用这个:

filter(isOfType(LoginActionTypes.LOGIN_ACTION))

向下传递的 Action 将被正确识别为 LoginAction .

关于reactjs - React + Redux-Observable + Typescript - 编译,参数不可分配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com