46

I updated rxjs from version 6.x.x to 7.x.x, but following error appeared:

Error in src/app/app.component.ts (12:19) Expected 1 arguments, but got 0.

when trying to next an empty value to the Subject

destroy$ = new Subject();

constructor() {
  this.destroy$.next(); // <-- Expected 1 arguments, but got 0.
}

Error stackblitz

eko
  • 39,722
  • 10
  • 72
  • 98
  • See https://github.com/ReactiveX/rxjs/issues/6324 or this https://stackoverflow.com/questions/68543205/argument-when-using-next-with-takeuntil – martin Jul 30 '21 at 12:26
  • @martin I was searching everywhere for this question! Thanks!, I'll duplicate this and provide an answer there. I think this title is more generic for people to find – eko Jul 30 '21 at 12:28

2 Answers2

85

tl;dr:

Either indicate the type explicitly with void:

new Subject<void>();

or pass a fake value:

this.destroy$.next(true);

Rxjs 7 changes

After checking the changelog and several github issues about this situation,

Subject: resolve issue where Subject constructor errantly allowed an argument (#5476) (e1d35dc)

Subject: no default generic (e678e81)

Changelog 7.0.0-beta.1 and the commit where empty value is removed from the tests

enter image description here

I realized that the solution was to either provide a value or simply typecast the Subject with <void> as in destroy$ = new Subject<void>() if you want to next it with an empty value.

eko
  • 39,722
  • 10
  • 72
  • 98
0

This is a new expected behavior change in the rxjs7 or above version as per the typescript. You need to pass some type or if you do not have any idea about the type.

option1: Pass void type

const sub = new Subject<void>();
sub.next();

option2: multiple types

new Subject<void | SomeOtherTypeHere>()
Muthukumar
  • 554
  • 4
  • 9
  • what is this answer adding to the already existing answer? multiple types? That's not what this question is about. Before rxjs 7, we were able to push `void` into the `Subject` but after 7 you can't do it without explicitly setting the type. If you were pushing anything else then `void` then you won't receive this error already so the multi type has nothing to do with this question. The other is just a copy paste from the accepted answer. Don't fish for upvotes please – eko Jul 21 '23 at 13:46