title: Why wont Typescript let BehaviorSubject take a union type?
body: <p>I believe this problem is purely Typescript related, and just happens to involve the BehaviorSubject in my case.</p>
  
  <p>I know that both these lines:</p>
  
  <pre><code>    const myVariable: string | null = 'string';
  </code></pre>
  
  <p>and</p>
  
  <pre><code>    const myVariable: string | null = null;
  </code></pre>
  
  <p>are valid syntax. So along the same lines I have declared this BehavoirSubject:</p>
  
  <pre><code>    const user: BehaviorSubject&lt;User | null&gt; = new BehaviorSubject(
        null
      );
  </code></pre>
  
  <p>This doesn't work, as this error:</p>
  
  <pre><code>Type 'BehaviorSubject&lt;null&gt;' is not assignable to type 'BehaviorSubject&lt;User | null&gt;'.
    Types of property 'observers' are incompatible.
      Type 'Observer&lt;null&gt;[]' is not assignable to type 'Observer&lt;User | null&gt;[]'.
        Type 'Observer&lt;null&gt;' is not assignable to type 'Observer&lt;User | null&gt;'.
          Type 'User | null' is not assignable to type 'null'.
            Type 'User' is not assignable to type 'null'.ts(2322)
  </code></pre>
  
  <p>My expectation is that this BehaviorSubject should take either User or null types in this example, but it will seemingly only accept both at the same time as a single expected type, hence this works as a workaround:</p>
  
  <pre><code>const user: BehaviorSubject&lt;User | null&gt; = new BehaviorSubject(
      null as User | null
    );
  </code></pre>
  
  <p>The types themselves don't seem to matter, "string | null" and "User | string " both throw the same kind of error, so how should this kind of typing actually be done? I'd rather not have to typecast ever. </p>
  
  <p>Thanks in advance :)</p>