title: Flowtype: How to create type guard function?
body: <p>I want to use <a href="https://flow.org/en/docs/lang/refinements/" rel="nofollow noreferrer">type refinements</a> from function.
  How to create <a href="https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html" rel="nofollow noreferrer">type guard</a> function (TypeScript) in flow?</p>
  
  <p><strong>I</strong> TypeScript example: </p>
  
  <pre><code>function isString(arg: Showable): arg is string {
      return typeof arg === 'string';
  }
  </code></pre>
  
  <p><strong>II</strong> Flow</p>
  
  <pre><code>/* @flow */
  type Showable = number | string;
  
  // ok
  function barOk (arg: Showable) {
    return typeof arg === 'string' ? arg.length : (arg + 1);
  }
  
  // type guard function
  function isString(arg: Showable) {
      return typeof arg === 'string';
  }
  
  // Error
  function barError (arg: Showable) {
    return isString(arg) ? arg.length : (arg + 1);
                           // ^ Cannot get `arg.length` because property `length` is missing in `Number`
  }
  </code></pre>