NgRX 效果 - 类型“Observable<unknown>"不可分配到类型“Observable<Action>" [英] NgRX effects - Type &#39;Observable&lt;unknown&gt;&#39; is not assignable to type &#39;Observable&lt;Action&gt;&#39;

查看:39
本文介绍了NgRX 效果 - 类型“Observable<unknown>"不可分配到类型“Observable<Action>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 NgRX 8 时,我和我的同事在实现效果时经常遇到奇怪的错误消息.

While working with NgRX 8 my colleagues and me are frequently facing a weird error message when implementing the effects.

类型 'Observable' 不可分配给类型 'Observable |((...args: any[]) => Observable)'

Type 'Observable<unknown>' is not assignable to type 'Observable<Action> | ((...args: any[]) => Observable<Action>)'

它与类型问题有关.消息如此不具体,它标志着完整的效果,这真的很烦人.这种情况经常出现,而且很难解决.

It is related to type issues. It is really annoying that the message is so unspecific and it marks the complete effect. This appears frequently and is really hard to resolve.

我们想知道是否可以采取一些措施来快速识别问题,或者我们是否能够以某种方式解构消息.我不是在这里寻找特定的解决方案,而是更多地寻找如何快速确定问题所在"的过程.

We are wondering if there is something we can do in order to quickly identify the problems or if we are able to destructure the message in some way. I am not looking for a specific solution here, but more for a "how to quickly determine what's wrong"-procedure.

谢谢和欢呼!

这是我们迄今为止所做的以及来自评论和答案的想法.

It is what we have done so far and also ideas from comments and answers.

  • 在大多数情况下,这可能不是操作的问题
  • switchMap 参数可能是一个错误的解构
  • 未导入其中一个 RxJS 运算符
  • 服务中错误的参数和返回值可能是原因
  • 服务和操作类型不一致也可能是原因
  • 如果没有任何帮助并且您确定没有问题:重新加载 TypeScript
  • In most cases it is probably not an issue with actions
  • It can be a wrong destructuring for the switchMap parameter
  • One of the RxJS operators is not imported
  • Wrong parameters and return value in the service can be the reason
  • Inconsistent types in service and action can be the reason as well
  • If nothing helps and you are sure, there is nothing wrong: Reload TypeScript

我们仍然希望有一个技巧来分解错误消息.

We are still hoping for a trick to break down the error message.

推荐答案

快速版
注释掉 createEffect(() =>,
修复您的 IDE (VSCode) 标记的错误,
重新添加 createEffect(() =>.

替代方案 - 像下面这样重写也有效

Alternative - rewriting like the following also works

someEffect$ = createEffect(() => {
  return this.actions$.pipe(
    ...
  )
})

附加

执行上述操作后仍然出错?类型检查是正确地完成它的工作,并告诉你你应该映射到 Observable 或添加第二个参数 { dispatch: false }(即不分派动作).请参阅NgRx 效果文档

Still errors after doing the above? Type-checking is doing it's job correctly and telling you that you should be mapping to an Observable<Action> or for a purely side-effect effect adding the second argument { dispatch: false } (i.e. not dispatching an action). See the NgRx Effects Docs

旧答案(使用 @Effect 是不必要的,不是必需的)

Older Answer (using @Effect is unneccessary and is not required)

我发现最简单的调试方法是使用 @Effect 装饰器以第 7 版的方式编写,一旦完成,使用 createEffect 重写.

The easiest way I've found to debug is to write in a version 7 manner with the @Effect decorator and once done rewrite using createEffect.

所以要调试:

  navigateToDashboard$ = createEffect(() =>
    this.actions$.pipe(
      ofType(teamActions.CREATE_SUPERVISOR_GROUP_SUCCESS),
      map((action: teamActions.CreateSupervisorGroupSuccess) => action.payload),
      map((team: Team) => team.TeamID),
      SwitchMap(id => new routerActions.Go({ path: ['/team', id, 'populate'] }))
    )
  )

将无用的错误写为(添加装饰器,删除 createEffect(() =>, 删除最后一个括号),

which gives the non-helpful error write as (add decorator, delete createEffect(() =>, delete final bracket),

@Effect()
navigateToDashboard$ = this.actions$.pipe(
    ofType(teamActions.CREATE_SUPERVISOR_GROUP_SUCCESS),
    map((action: teamActions.CreateSupervisorGroupSuccess) => action.payload),
    map((team: Team) => team.TeamID),
    SwitchMap(id => new routerActions.Go({ path: ['/team', id, 'populate'] }))
)

现在我们得到错误

Cannot find name 'SwitchMap'

关注

Type 'Go' is not assignable to type 'ObservableInput<any>'

解决这个问题

@Effect()
navigateToDashboard$ = this.actions$.pipe(
    ofType(teamActions.CREATE_SUPERVISOR_GROUP_SUCCESS),
    map((action: teamActions.CreateSupervisorGroupSuccess) => action.payload),
    map((team: Team) => team.TeamID),
    switchMap(id => of(new routerActions.Go({ path: ['/team', id, 'populate'] })))
)

现在用 NgRx 8 术语重写.不漂亮但有效.

Now rewrite in NgRx 8 terms. Not pretty but works.

这篇关于NgRX 效果 - 类型“Observable<unknown>"不可分配到类型“Observable<Action>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆