62

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.

Not deprecated:

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

Is marked deprecated:

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

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.

4
  • 9
    No. github.com/ReactiveX/rxjs/issues/4772
    – cartant
    Commented Jun 12, 2019 at 23:55
  • @cartant ah. I see now. That should be answer if you want.
    – Reactgular
    Commented Jun 13, 2019 at 0:06
  • 2
    Pressed for time. If you could self answer your question, I'd appreciate it. I've added a note to my TODO list to improve the deprecation messages. And there's this issue, too: github.com/ReactiveX/rxjs/issues/4776
    – cartant
    Commented Jun 13, 2019 at 0:50
  • @cartant no problem. thanks for all the great work on rxjs.
    – Reactgular
    Commented Jun 13, 2019 at 13:08

8 Answers 8

72

No, it is not.

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

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.

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

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()).


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.

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.

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

3
  • 14
    For others finding this when using null as start value the typescript compiler thinks you might be using the overload startWith<T>(scheduler: SchedulerLike) which is why there is a warning.
    – ntziolis
    Commented Aug 4, 2020 at 5:31
  • 3
    I've found that error using startWith(null) should I remove startWith in that case?
    – BruneX
    Commented Sep 22, 2020 at 18:12
  • 4
    If you have the warning due to using startWith(null) or startWith(undefined), then the warning is erroneous and may be ignored. But you may fix it by casting null or undefined to some type. It may be some type of the related Observable, or just some unrelated type, e.g. startWith(<string>null). Edited: actually Anh-Thi DINH already posted it below.
    – Alex Che
    Commented Aug 6, 2021 at 11:23
22

For ones who see the deprecated warning in VSCode when you are using startWith(null), just replace it with startWith(<string>null) in order to resolve the warning message.

More information is here.

10

One way to avoid the deprecation notice is by typecasting whatever you're passing into startWith. For example startwith(x as boolean) in the OP's example.

This way, you're reassuring your IDE that you're not using a deprecated signature.

2

I also got a deprecated message when I was trying to startWith(undefined) The reason is it's defaulting to export declare function startWith<T>(scheduler: SchedulerLike): MonoTypeOperatorFunction<T>; the deprecated API

The fix is specifying the return type D(of the undefined): export declare function startWith<T, D>(v1: D): OperatorFunction<T, T | D>;

For example let's say I have Interface MyType1 and my observable maps it to myType2: startWith<MyType1, MyType1>(undefined)

1

This trick works very well:

startWith<void, void>(undefined);

1
  • Getting this error for the mentioned approach, Argument of type 'OperatorFunction<void, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>'. Type 'void' is not assignable to type 'Object'. Commented Jun 2, 2022 at 14:06
0

My current alternative for a specific case (text input element) is to use an empty string ''. It's also a falsy value. startWith(''). But the observable event is from type any. So null should also accepted. (Background: I use Angular Material "FormControl" valueChanges.)

0

Deprecation message is coming from signature

export function startWith<T, D = T>(...array: Array<D | SchedulerLike>): OperatorFunction<T, T | D>;

This has been removed with version 7 of RxJS. In the mean time you can refer to

export function startWith<T, D>(v1: D): OperatorFunction<T, T | D>;

which takes a v1 of type D and return (in the pipe chain) a T value.

In the end your code will be

 of(false).pipe(startWith<boolean, boolean>(x));
-1

Replaced

startWith(null as any) 

with

startWith<null, null>(null)

for better readability.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.