RXJS中的startWith运算符真的过时了吗? [英] Is the startWith operator in RXJS really deprecated?

查看:98
本文介绍了RXJS中的startWith运算符真的过时了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从更新到RXJS版本6以来,我的WebStorm编辑器一直在抱怨 ="使用 startWith(),该操作符被标记为已弃用.

Since updating to RXJS version 6 my WebStorm editor has been complaining on some usages of startWith() that the operator is marked as deprecated.

您可以在源代码中看到这些方法被标记为已弃用:

You can see in the source code that the methods are marked deprecated:

对我来说,问题是不赞成使用的警告不一致.有时它报告不推荐使用的方法,而其他时候则不报告.虽然我可以在下面的代码示例中重现警告.这似乎是在我自己的源代码中随机发生的.

The problem for me is that the deprecated warning is not consistent. Sometimes it reports the method deprecated and other times it does not. While I can reproduce the warning in the below code examples. It seems to happen in my own source code randomly.

不推荐使用:

  of(false).pipe(startWith(true));

已标记为已弃用:

  const x: any = true;
  of(false).pipe(startWith(x));

因此,我担心这些已过时的警告.不赞成使用的消息说,改用 scheduled() concat()运算符,但是对于像 startWith()这样已经很方便的运算符,这感觉像是一个更复杂的选择.

So I am worried about these deprecated warnings. The deprecation message says to use scheduled() and concat() operators instead, but that feels like a more complicate alternative to an already handy operator like startWith().

所以我对为什么不推荐使用它感到困惑,但是为什么有时只不推荐使用它.

So I'm kind of confused as to why it's deprecated, but also why it's only deprecated sometimes.

推荐答案

不,不是.

当前只有一个活动签名:startWith(... values)

Currently there is only one active signature: startWith(...values)

除此签名外,它还有几个重载,它们接受 scheduler:SchedulerLike 作为最新参数:

Apart from this signature, it has several overloads which accept scheduler: SchedulerLike as the latest parameter: startWith(...values, scheduler) and this functionality has been deprecated.

如果您不将 scheduler startWith 一起使用,就可以了.

If you don't use scheduler with startWith you are fine.

如果这样做,则需要使用 scheduled 函数重写代码,就像它们在折旧注释旁边的注释中建议的那样: scheduled([[a,b,c],source],scheduler).pipe(concatAll()).

If you do, then you need rewrite your code using scheduled function like they suggest in the comment beside depreciation annotation: scheduled([[a, b, c], source], scheduler).pipe(concatAll()).

很有可能您正在使用 startWith(null) startWith(undefined),尽管有此通知,它们仍不被弃用,但是IDE检测到错误的函数签名,这会导致已弃用,并显示警告.

Highly likely, you are using startWith(null) or startWith(undefined), they are not deprecated despite the notice, but IDE detects a wrong function signature, which is deprecated, and shows the warning.

或者,您正在使用 formControl.valueChanges 发出 any 类型,或任何其他带有 any 的可观察流.由于 any SchedulerLike 相匹配,因此您会看到该通知.

Or, you are using formControl.valueChanges which emits any type, or any other observable stream with any. Because any matches the SchedulerLike, you see the notice.

因此,尝试通过添加 filter((v):v is number => typeof ==='number')或任何其他可能的方法来避免 any

Therefore, try to avoid any via adding filter((v): v is number => typeof === 'number') or any other possible way.

这篇关于RXJS中的startWith运算符真的过时了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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