title: Typescript: type guard across properties in an interface
body: <p>In typescript I am able to declare a function type-guard to check if a property is null or not similarly to the following:</p>
  <pre class="lang-js prettyprint-override"><code>interface IApiResponse&lt;TRecord&gt; {
    readonly isDefined: boolean;
    readonly record: TRecord | null;
  }
  
  const responseHasRecordDefined = 
    &lt;TRecord&gt;(apiResponse: IApiResponse&lt;TRecord&gt;): apiResponse.record is TRecord =&gt; apiResponse.isDefined;
  </code></pre>
  <p>However I was wondering if there is a way to accomplish the same task with just a single interface definition?</p>
  <p>Like something similar to:</p>
  <pre class="lang-js prettyprint-override"><code>interface IApiResponse&lt;TRecord&gt; {
    readonly record: TRecord | null;
    readonly isDefined: this.record is TRecord;
  }
  </code></pre>
  <p>EDIT:</p>
  <p>As Ryan pointed out in the comments this initial case is easily handled by a null check. So I'm adding another example of a problem that requires a type guard of some sort rather than a simple null check.</p>
  <pre class="lang-js prettyprint-override"><code>interface IResponseTypeA {
    // Anything can be in here
  }
  
  interface IResponseTypeB {
    // Anything can be in here
  }
  
  interface IApiResponse {
    readonly record: IResponseTypeA | IResponseTypeB;
    readonly isTypeA: this.record is IResponseTypeA;
  }
  </code></pre>