title: Override function return type in typescript
body: <p>Is there a way to override a function's return type? Using typescript <code>3.1.6</code> at the time of writing.</p>
  
  <p>Contrived example, but to get the point across:</p>
  
  <pre><code>function sample(foo): string | number {
      if (foo === 'foo') {
          return 'string'
      }
  
      return 1
  }
  
  const result = sample('bar')
  
  // but since we know it's a number we should be able to tell typescript this
  const typedResult: number = sample('bar')
  </code></pre>
  
  <p>In reality, the logic in the function might be complex.</p>
  
  <p><a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html" rel="nofollow noreferrer">keyof and lookup types</a> looked promising, but I'm not working with a limited set of options nor can I deduce a pattern reliably.</p>
  
  <p>I've also tried the following workaround, but it didn't work. It seems like you can't override a type.</p>
  
  <pre><code>const result = sample('bar')
  const typedResult: number = result
  </code></pre>
  
  <p>What's frustrating about this is that even if I do the proper checks to accurately check what type I'm dealing with, I still get type errors if I use methods exclusive to that type. Typescript still thinks we don't 100% know the type.</p>
  
  <p>Any leads or solutions to this?</p>