title: Check API response data with interface in Typescript Angular
body: <p>I have an app that when the user logs in he received some data as a response, but if the response is not what I expect, the app crashes.</p>
  
  <p>Let me show you the following:</p>
  
  <p>I created an interface to tell Typescript the data I expect.</p>
  
  <pre class="lang-js prettyprint-override"><code>export interface AuthResponseData {
    token: string;
    expires: string;
    role: number;
  }
  </code></pre>
  
  <p>Then I created a method to send the login data to the server.</p>
  
  <pre class="lang-js prettyprint-override"><code>login(user: string, password: string){
    return this.http.post&lt;AuthResponseData&gt;(
      'https://localhost:44344/api/auth', { user: user, pwd: password }
    ).pipe(
      catchError(this.handleError),
      tap(resData =&gt; { this.handleAuthentication(resData.token, resData.expires, resData.role)})
    );
  }
  </code></pre>
  
  <p>I was expecting that if the response from the server didn't match the interface then the Angular would redirect to <code>catchError</code>. But doesn't happen. </p>
  
  <p><strong>Now my question is</strong>: Is there any way to check if an API response is equal to the interface and throw an error if is not. Or what I'm asking isn't possible?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After searching I discover that the interfaces disappear on run time, so there is no way to compare the api response with the interface (to the best of my knowledge). But I keep searching for a way to check the api response in a dynamic way. I think it is not really secure depending on api response to be always right. So I should check the API response. How can I do that?</p>
  
  <p>Hope you can help me, thanks :)</p>