rxjs - איך להעביר פעולות NGRX לפונקציות כפרמטרים ב-TypeScript?
יש לי תנאי if else די גדול באפליקציית Angular 7 שלי, שבה אני משתמש במנהל מצב ו-rxjs, ואני רוצה להפוך את הקוד ליותר קריא. בתנאים שונים אני צריך לשלוח פעולות שונות. החלטתי לצמצם את בלוק ה-if else על ידי העברת חלקים לפונקציה שניתנת לשימוש חוזר, ואני צריך להעביר פעולות NGRX לאותן פונקציות כפרמטרים. אבל TypeScript נותן שגיאה: הפרמטר 'xxx' יש לו באופן מרומז טיפוס 'any'.
השאלה היא - איזה סוג אני צריך להגדיר לפעולות? כי setSomeDataAction: Action - לא עובד
קוד עכשיו:
if (mobile) {
if (newVisitor) {
this.store.dispatch(SetMobileForNewVisitor(payload));
} else {
this.store.dispatch(SetMobileForExistingVisitor(payload));
}
} else {
if (newVisitor) {
this.store.dispatch(SetNotMobileForNewVisitor(payload));
} else {
this.store.dispatch(SetNotMobileForExistingVisitor(payload));
}
}
קוד שמפיל שגיאת TypeScript
mobile ?
this.dispatchRemoveOne(SetMobileForNewVisitor, SetMobileForExistingVisitor, payload) :
this.dispatchRemoveOne(SetNotMobileForNewVisitor,SetNotMobileForExistingVisitor, payload);
private dispatchRemoveOne(newVisitorAction, existingVisitorAction, payload) {
newVisitor
? this.store.dispatch(newVisitorAction(payload))
: this.store.dispatch(existingVisitorAction(payload));
}
ניסיתי משהו כזה:
private dispatchRemoveOne(newVisitorAction: Action, existingVisitorAction: Action, payload: Payload[]) {
newVisitor
? this.store.dispatch(newVisitorAction(payload))
: this.store.dispatch(existingVisitorAction(payload));
}