title: Why isn't this type guard for string working?
body: <p>I have a TypeScript method which accepts a parameter. The parameter can be either of type <code>string</code> or type <code>Object</code>. When I use a <code>typeof</code> check for type <code>string</code> in the method body, I still get error stating that <code>indexOf</code> and <code>substr</code> are not found on type <code>string | Object</code>.</p>
  
  <p>As far as I understand the <code>if</code> block body should be type-guarded by the condition. What am I doing wrong?</p>
  
  <pre><code>function mergeUrlParameters(sourceParameters: string | Object, targetUrl: string, overwrite: boolean = false): string {
  
    var sourceParamObject: Object;
    if (typeof sourceParameters === "string") {
      if (sourceParameters.indexOf("?") &gt;= 0)
        sourceParameters = sourceParameters.substr(sourceParameters.indexOf("?") + 1);
      sourceParamObject = $.deparam(sourceParameters, true);
    } else {
      sourceParamObject = sourceParameters;
    }
  
    ...
  }
  </code></pre>
  
  <p>I'm using Visual Studio 2015 with latest TypeScript installed (1.8.x).</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It turned out that the reassignment of the <code>sourceParameters</code> variable causes the type-guard to fail. I've posted a related potential bug report about this behavior <a href="https://github.com/Microsoft/TypeScript/issues/8022" rel="nofollow">here</a>. In the meantime, several easy workarounds are available, I chose to inline the check and substring call with a <code>?</code> conditional expression.</p>