rxjs - TypeScriptでNGRXのアクションを関数のパラメータとして渡す方法は?
私のAngular 7アプリケーションには少し大きな if else 条件があります。ここではStateマネージャーと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));
}