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

查看:11
本文介绍了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 作为最新参数:startWith(...values, scheduler) 并且此功能已被弃用.

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.

如果您不将 schedulerstartWith 一起使用,那就没问题了.

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 的可观察流.由于 anySchedulerLike 匹配,您会看到通知.

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天全站免登陆