Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error when using Epic with AsyncAction #223

Open
2 tasks
EmmanueleVilla opened this issue Jan 27, 2020 · 2 comments
Open
2 tasks

Type error when using Epic with AsyncAction #223

EmmanueleVilla opened this issue Jan 27, 2020 · 2 comments

Comments

@EmmanueleVilla
Copy link

Description

When intercepting an asyncAction with an epic, I got a compiler error that it's also present on your sample application in the codesandbox

Mandatory info

How to Reproduce

CodeSandbox Link

I do not have a personal codesandbox link, but the exact same error is present in your codesandbox link (https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox) in the file src/feature/todos/epic:

export const loadTodosEpic: Epic<
  RootAction,
  RootAction,
  RootState,
  Services
> = (action$, state$, { api }) =>
  action$.pipe(
    filter(isActionOf(loadTodosAsync.request)),
    switchMap(() =>
      from(api.todos.loadSnapshot()).pipe(
        map(loadTodosAsync.success),
        catchError((message: string) => of(loadTodosAsync.failure(message)))
      )
    )
  );
Il tipo 'Observable<unknown>' non è assegnabile al tipo 'Observable<RouterAction | PayloadAction<"ADD_TODO", Todo> | PayloadAction<"REMOVE_TODO", string> | PayloadMetaAction<"LOAD_TODOS_REQUEST", unknown, unknown> | PayloadAction<"LOAD_TODOS_SUCCESS", Todo[]> | PayloadAction<...> | PayloadMetaAction<...> | PayloadMetaAction<...> | PayloadAction<...>>'.
  Il tipo 'unknown' non è assegnabile al tipo 'RouterAction | PayloadAction<"ADD_TODO", Todo> | PayloadAction<"REMOVE_TODO", string> | PayloadMetaAction<"LOAD_TODOS_REQUEST", unknown, unknown> | PayloadAction<"LOAD_TODOS_SUCCESS", Todo[]> | PayloadAction<...> | PayloadMetaAction<...> | PayloadMetaAction<...> | PayloadAction<...>'.
    Type '{}' is missing the following properties from type 'PayloadAction<"SAVE_TODOS_FAILURE", string>': type, payloadts(2322)
index.d.ts(36, 26): The expected type comes from the return type of this signature.

Expected behavior

Typescript compiles since the return type is correct

Suggested solution(s)

Project Dependencies

  • Typesafe-Actions Version: 5.1.0
  • Redux Version: 4.0.5
  • Redux-Observable Version: 1.2.0
  • RxJS Version: 6.5.4
  • TypeScript Version: 3.7.5
  • tsconfig.json:
{
  "compilerOptions": {
    "target": "es5" 
    "module": "commonjs" 
    "outDir": "generated" 
    "strict": true 
    "esModuleInterop": true 
  }
}
@EmmanueleVilla
Copy link
Author

Update: it seems that using "switchMap" it's what's causing the error.

As per the issue description, this doesn't work:

export const test1: Epic<RootAction, RootAction, RootState> = (action$, _state$, {}) => {
  return action$.pipe(
    filter(isActionOf(homeActions.homeLoadListingAction.request)),
    switchMap(_action => of(homeActions.homeLoadSectionAction.success([]))),
  );
};

I've tried to remove the filter and it still doesn't work:

export const test2: Epic<RootAction, RootAction, RootState> = (action$, _state$, {}) => {
  return action$.pipe(
    switchMap(_action => of(homeActions.homeLoadSectionAction.success([]))),
  );
};

But if I remove the switchMap, it works:

export const test1: Epic<RootAction, RootAction, RootState> = (action$, _state$, {}) => {
  return of(homeActions.homeLoadSectionAction.success([]));
};

So the return type seems correct

@EmmanueleVilla
Copy link
Author

Update:
I've changed the tsconfig from

"target": "es5"

to

"target": "esnext"

and now it works. Do you know why it doesn't work on es5? Should it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant