Search code examples
javascripttypescriptrxjsbehaviorsubject

Is it possible to create BehaviourSubject<void>() without initial value?


I'm using a BehaviourSubject purely as trigger, so the payload doesn't matter. The subject should "fire" (call next) on subscription (a simple Subject wouldn't work!). In principle it would be enough to have a BehaviourSubject<void> for this purpose:

const readonly trigger = new BehaviourSubject<void>()
                                                   ^^
                                                   must not be empty

However, it seems to be impossible to create such a subject. Or is there a way to create a BehaviourSubject of type void without an initial value?


Solution

  • clearly Subject doesn't meet your needs as you want a trigger on subscription. you can set the initial value to undefined though...

    new BehaviorSubject<void>(undefined)
    

    or null should work fine too.