piotrwitek/typesafe-actions

Request: Example of Reducer for createAsyncAction #68

daveking999 posted onGitHub

I would like to expand your Todo example by providing an action which fetches a list of Todo's from an API.

I've declared the following in actions.ts:

const FETCH = 'todos/FETCH';
const FETCH_SUCCESS = 'todos/FETCH_SUCCESS';
const FETCH_FAILURE = 'todos/FETCH_FAILURE';

export const fetchTodos = createAsyncAction(
    FETCH,
    FETCH_SUCCESS,
    FETCH_FAILURE
)<void, Todo[], Error>();

and declared the API in root-epic.ts

const fetchTodosFlow: Epic<RootAction, RootState> = (action$, store) =>
    action$.pipe(
        filter(isActionOf(fetchTodos.request)),
        mergeMap(action =>
            ajax.getJSON('http://someurl/fetch').pipe(
                map(fetchTodos.success),
                catchError(pipe(fetchTodos.failure, of))
            )
        ));

however I get the following tslint error:

Error:(14, 7) TS2322: Type '(action$: ActionsObservable<RootAction>, store: StateObservable<{ router: RouterState; todos: Readonly<{ todos: Todo[]; todosFetch: Todo[]; todosFilter: TodosFilter; }>; }>) => Observable<{} | PayloadAction<...>>' is not assignable to type 'Epic<RootAction, { router: RouterState; todos: Readonly<{ todos: Todo[]; todosFetch: Todo[]; todosFilter: TodosFilter; }>; }, any, RootAction>'.
  Type 'Observable<{} | PayloadAction<"todos/FETCH_SUCCESS", Todo[]>>' is not assignable to type 'Observable<RootAction>'.
    Type '{} | PayloadAction<"todos/FETCH_SUCCESS", Todo[]>' is not assignable to type 'RootAction'.
      Type '{}' is not assignable to type 'RootAction'.
        Type '{}' is not assignable to type 'LocationChangeAction'.
          Property 'type' is missing in type '{}'.

I'm using the following versions:

    "react-redux": "5.0.7",
    "react-router-redux": "5.0.0-alpha.9",
    "redux": "4.0.0",
    "redux-observable": "^1.0.0",
    "redux-thunk": "^2.3.0",
    "rxjs": "6.1.0",
    "rxjs-compat": "6.1.0",

Any help is greatly appreciated.


Fund this Issue

Rewarded pull request

Recent activities

piotrwitek was rewarded for piotrwitek/typesafe-actions# 68
over 6 years ago
piotrwitek submitted an output to  piotrwitek/ typesafe-actions# 68
over 6 years ago