使用TypeScript的combineEpics会出现类型错误

时间:2017-11-10 16:55:09

标签: typescript rxjs redux-observable

我在结合我的史诗时遇到了这个错误:

TS2345: Argument of type 'Epic<SessionAction, GlobalState, any>' is not assignable to parameter of type 'Epic<EmployeeAction, GlobalState, any>'.
Type 'SessionAction' is not assignable to type 'EmployeeAction'.
Types of property 'type' are incompatible.
  Type 'SessionActionTypes' is not assignable to type 'EmployeeActionTypes'.

以下是代码:

import { combineEpics } from 'redux-observable';
import { fetchUserSession } from './sessionEpics';
import { fetchEmployee } from './employeeEpics';

export default combineEpics(
  fetchEmployee,
  fetchUserSession
);

3 个答案:

答案 0 :(得分:2)

我有同样的问题。似乎打字稿抓住了第一部史诗,推断出它的类型,并期望其余部分都是相同的。

我做的是投出第一个。

以您的代码为例:

import { Observable } from 'rxjs/Observable';
import { AnyAction } from 'redux';
import { combineEpics } from 'redux-observable';
import { fetchUserSession } from './sessionEpics';
import { fetchEmployee } from './employeeEpics';

export default combineEpics(
  fetchEmployee as Observable<AnyAction>,
  fetchUserSession
);

答案 1 :(得分:1)

或使用combineEpics<any>

export default combineEpics<any>(
  fetchEmployee,
  fetchUserSession
);

答案 2 :(得分:0)

更好的解决方案可能是使用正确的类型:Observableimport { AnyAction } from "redux" import { combineEpics, createEpicMiddleware } from "redux-observable" import { Observable } from "rxjs" export const clientMiddleware = createEpicMiddleware( combineEpics<AnyAction, Observable<AnyAction>>( authenticateEpic(request), registerEpic(request) ))

public function article_children() {
    return $this->hasMany(self::class, 'parent_id',self::PRIMARY_KEY);
}