title: TypeScript parameter type is a union of multiple types: How can I determine which was supplied and work with it?
body: <p>I'm working with the <a href="https://github.com/anthonyjgrove/react-google-login" rel="nofollow noreferrer">React Google Login</a> library in TypeScript.  It has type bindings for TypeScript, but all the examples are in JavaScript, and I am pretty new to TypeScript.</p>
  
  <p>The setup code looks like this:</p>
  
  <pre><code>  &lt;GoogleLogin
      clientId="client-id-value"
      onSuccess={successResponseGoogle}
      onFailure={failureResponseGoogle}
      cookiePolicy={'single_host_origin'}
    /&gt;
  </code></pre>
  
  <p>In TypeScript, the signature for the onSuccess callback is:</p>
  
  <pre><code>readonly onSuccess: (response: GoogleLoginResponse | GoogleLoginResponseOffline) =&gt; void
  </code></pre>
  
  <p>The <code>GoogleLoginResponseOffline</code> type has only one property, <code>code</code>, where <code>GoogleLoginResponse</code> has a range of properties to access the details of the authenticated user.  </p>
  
  <p>The problem that I am having is that TypeScript will not let me access any of the GoogleLoginResponse properties on the response parameter, saying, for example </p>
  
  <blockquote>
    <p><em>"Property 'getBasicProfile' does not exist on type GoogleLoginResponseOffline'"</em></p>
  </blockquote>
  
  <p>I have tried the following to cast or check the type of the parameter, but all give errors of one type or another.  My function looks like this:</p>
  
  <pre><code>const responseGoogleSuccess = (response: GoogleLoginResponse|GoogleLoginResponseOffline) =&gt; {
  
    // Tried to check for property to identify type
    if(response.googleId){    // Property 'googleId' does not exist on type 'GoogleLoginResponseOffline'
        const profile = response.getBasicProfile();  // Property 'getBasicProfile' does not exist on type 'GoogleLoginResponseOffline'
    }
  
    // Tried to cast like this
    const typedResponse = &lt;GoogleLoginResponse&gt;response;
  
    // Tried to check type
    if(response instanceof GoogleLoginResponse){   // 'GoogleLoginResponse' only refers to a type, but is being used as a value here.
    }
  }
  </code></pre>
  
  <p>It looks from the <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#instanceof-type-guards" rel="nofollow noreferrer">TypeScript documentation</a> as if the <code>if(response instanceof GoogleLoginResponse)</code> is close, fails in this case because <code>GoogleLoginResponse</code> is an interface and it needs to be a class.</p>
  
  <p>Please tell me how this is done!  I've looked at lots of StackOverflow questions with similar titles, but none cover this.</p>