title: Typescript - return type conditions
body: <p>This question is focused on Typescript usage in React.</p>
  <p>I have two React hooks: <code>useStatus(): Status</code> and <code>useResult(): Result | null</code> and use them like:</p>
  <pre><code>const status = useStatus();
  const result = useResult();
  
  if (status !== &quot;success&quot;) {
    return &lt;Loading /&gt;;
  }
  
  return ... something using result ...;
  </code></pre>
  <p>I can change the signature of both functions (parameters and return type).
  I can't change the position of the <code>useResult</code> call (it has to be before <code>if</code>) - that's React's only constraint.</p>
  <p>Conceptually, I know <code>result</code> will not be <code>null</code> if <code>status</code> is <code>&quot;success&quot;</code>. I'd like to somehow encode this information in return type. That way, Typescript would somehow know at last line, that <code>status</code> is <code>&quot;success&quot;</code> so <code>result</code> is <code>Result</code> (not <code>Result | null</code>).</p>
  <p>I know I can separate in two different components - that's what I'm already doing, but curious if I can avoid that.</p>
  <p>Is there a way to achieve this?</p>
  <p>Thanks! :)</p>