我正在尝试使用 fp-ts 和 redux-observable 来构建一些处理一些 api 请求的史诗。我遇到了fp-ts-rxjs/ObservableEither#fold的问题,如果我不将我的操作转换为类型AnyAction
,我会收到一个类型错误,指出这两种类型应该是相同的。
Type 'Observable<{ payload: { user: User<Attributes>; }; type: string; }>' is not assignable to type 'Observable<{ payload: { error: Error | null; }; type: string; }>'.
Type '{ payload: { user: User<Attributes>; }; type: string; }' is not assignable to type '{ payload: { error: Error | null; }; type: string; }'.
Types of property 'payload' are incompatible.
Property 'error' is missing in type '{ user: User<Attributes>; }' but required in type '{ error: Error | null; }'
我也尝试过使用fp-ts-rxjs/ObservableEither#bimap因为它期望返回两种不同的类型。但是,这会导致运行时错误,指出操作不能具有未定义的类型。我也不确定那里到底发生了什么。
LoginSlice.ts
const loginSlice = createSlice({
name: 'login',
initialState,
reducers: {
loginSuccess (state, action: PayloadAction<{ user: User }>) {
state.loggedIn = true;
},
loginReset (state) {
state.error = null;
},
loginFail (state, action: PayloadAction<{ error: Error | null } >) {
state.error = action.payload.error;
}
}
});
LoginService.ts
const loginService = (credentials: LoginInfo): OE.ObservableEither<Error, User> => {
const { username, password } = credentials;
return OE.fromTaskEither(
TE.tryCatch(
async () => await User.logIn(username, password),
error => error as Error
)
);
};
LoginEpics.ts
export const loginEpic: Epic = (action$: ActionsObservable<AnyAction>) => action$.pipe(
filter(login.match),
mergeMap((action) =>
loginService(action.payload.credentials).pipe(
fold(
(error) => of(loginFail({ error }) as AnyAction),
(user) => of(loginSuccess({ user }) as AnyAction)
)
)
)
);
有没有办法避免将动作转换成AnyAction
? 任何见解将不胜感激。