typescript “MonoTypeOperatorFunction<any>”类型的参数不可分配给“UnaryFunction<Obs​​ervable<any>, Observable<any>>”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51646369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:33:45  来源:igfitidea点击:

Argument of type 'MonoTypeOperatorFunction<any>' is not assignable to parameter of type 'UnaryFunction<Observable<any>, Observable<any>>'

angulartypescriptionic-frameworkrxjs

提问by Yushin

i am trying to migrate from rxjs5 to 6 but i am having difficulties. when i try this

我正在尝试从rxjs5迁移到 6,但我遇到了困难。当我尝试这个时

this._connectivity.isOnline().pipe(first()).subscribe((state) => {
  this.syncCheck(user.uid);
});

i am getting this error

我收到这个错误

Argument of type 'MonoTypeOperatorFunction<any>' is not assignable to parameter of type 'UnaryFunction<Observable<any>, Observable<any>>'.
  Types of parameters 'source' and 'source' are incompatible.
    Type 'import("/home/User/Desktop/projectname/node_modules/rxjs/Observable").Observable<any>' is not assignable to type 'import("/home/User/Desktop/projectname/node_modules/rxjs/internal/Observable").Observable<a...'.
      Property 'map' is missing in type 'Observable<any>'.

采纳答案by Kamil Naja

I found the same error with my code:

我在我的代码中发现了同样的错误:

let source = of([1, 2, 3, 45, 56, 7, 7])
    .pipe(
        filter((x: number) => x % 2 == 0)
    );

TS2345: Argument of type 'MonoTypeOperatorFunction' is not assignable to parameter of type 'OperatorFunction'.

TS2345:“MonoTypeOperatorFunction”类型的参数不可分配给“OperatorFunction”类型的参数。

To fix this, remove type from filter function

要解决此问题,请从过滤器功能中删除类型

 filter(x => x % 2 == 0)

Now you have error

现在你有错误

The left-hand side of an arithmetic operation must be of type 'any', 'number', so make sure, that this annoying filter gets correct data type

算术运算的左侧必须是“any”、“number”类型,因此请确保这个烦人的过滤器获得正确的数据类型

filter(x => Number(x) % 2 == 0) // parse element to number

But now code stops work. Finally, to fix this, change of to from, at the beginning.

但现在代码停止工作。最后,为了解决这个问题,在开始时更改为。

let source = from([1, 2, 3, 45, 56, 7, 7])
    .pipe(
        filter((x: number) => Number(x) % 2 === 0)
    )

or

或者

let source = of(1, 2, 3, 45, 56, 7, 7)
.pipe(
        filter((x: number) => Number(x) % 2 === 0)
    )

So, cause of error was my initial data structure.

所以,错误的原因是我的初始数据结构。

I think, that my example can help you with dealing similar problems.

我认为,我的例子可以帮助你处理类似的问题。

回答by ggradnig

You seem to have a wrong import for the isOnline()return type. Make sure that you always import from rxjsand never from rxjs/internalor even rxjs/internal/Observable. (Operators like first()must be imported from rxjs/operators)

您似乎对isOnline()返回类型有错误的导入。确保您始终导入 fromrxjs并且从不导入,rxjs/internal甚至从rxjs/internal/Observable. (操作符first()必须从 导入rxjs/operators

回答by mpro

I had similar error using pipe in one of HTTP services method (see example on the bottom). The problem was lack of typing in callback function used in subscribe. In your case, add typing (not the best idea but even any) to stateor remove the parenthesis:

我在 HTTP 服务方法之一中使用管道时遇到了类似的错误(请参阅底部的示例)。问题是没有输入订阅中使用的回调函数。在您的情况下,将打字(不是最好的主意,甚至是any)添加到state或删除括号:

Solution 1:

解决方案1:

this._connectivity.isOnline()
  .pipe(first()).subscribe((state: any) => {
    this.syncCheck(user.uid);
});

Solution 2:

解决方案2:

this._connectivity.isOnline()
  .pipe(first()).subscribe(state => {
    this.syncCheck(user.uid);
});

Coming back to my case I've used pipe to log log the data. It required to set two possible types to the observable, then typing callback function was possible:

回到我的案例,我使用管道来记录数据。它需要为 observable 设置两种可能的类型,然后输入回调函数是可能的:

  public getPrice(): Observable<string | PriceDTO> {
    return this.http.get<PriceDTO>(`api/price)
      .pipe(
        tap((data: PriceDTO) => console.log('PRICE: ', data)),
        catchError((val: Error) => of(`I caught: ${val}`))
      );
  }

回答by lingar

I have such issue, first scan wasn't working, so I changed

我有这样的问题,第一次扫描不起作用,所以我改变了

  .pipe(scan((count ) => count + 1, 0))...

To

  .pipe(scan((count:any ) => count + 1, 0))

Then the problem also was that the Rxjs was installed also at the parent folder of this app. So removing this,I believe have fix the problem.

然后问题还在于 Rxjs 也安装在此应用程序的父文件夹中。所以删除这个,我相信已经解决了这个问题。

Using npm-checkpackage is good idea for cleaning problem either global and local level of npm packages.

使用npm-check包是清理 npm 包全局和本地级别问题的好主意。