title: Check type of object returned by Observable in Angular?
body: <p>I'm making an Angular Application which is sending calls to API. This is one of methods from AuthService (responsible for authenticating user):</p>
  <pre><code>login(loginForm: UserLoginRequest): Observable&lt;AuthenticationResponse | TfaResponse&gt; {
      return this.http.post&lt;AuthenticationResponse | TfaResponse&gt;('api/auth/login', loginForm);
  }
  </code></pre>
  <p>This methods return an Observable with one of following responses:</p>
  <pre><code>export class AuthenticationResponse {
      token: string;
      refreshToken: string;
  }
  
  export class TfaResponse {
      requiresTwoFactor: boolean;
      isTwoFactorConfigured: boolean;
  }
  </code></pre>
  <p>Then I subscribe on this Observable in my component and I want to do different actions in dependence of type of response:</p>
  <pre><code>this.authService.login(userLoginForm)
      .subscribe(
        (result) =&gt; {
          // type of result is AuthenticationResponse
          if (*type check*) {
              // action with AuthenticationResponse
          }
          // type of result is TfaResponse
          else if (*type check*) {
              // action with TfaResponse
          }
        },
      );
  </code></pre>
  <p>At the moment I'm just checking if object owns some properties from this class, like this:</p>
  <pre><code>this.authService.login(userLoginForm)
          .subscribe(
            (result) =&gt; {
              if ('token' in result) {
                  // action with AuthenticationResponse
              }
              else if ('requiresTwoFactor' in result) {
                  // action with TfaResponse
              }
            },
          );
  </code></pre>
  <p>But I understand that's a definitely bad solution which will broke up after first change in AuthenticationResponse or TfaResponse class. How should I check class in this case?</p>