title: Assign Typescript type in switch statement
body: <p>Try to write a generic function function to return <code>d3 scale</code>. but getting the following error as it is reading the wrong type after the <code>switch</code> statement.</p>
  
  <pre><code>import * as D3Scale from 'd3-scale';
  
  enum scaleIdentites {
  linear,
  time,
  }
  
  interface ScaleProps {
      scaleIdentity: scaleIdentites;
      range: number[];
      domain: number[];
  }
  
  export const scale = ({ scaleIdentity, domain }: ScaleProps) =&gt; {
      let scaleFunction: D3Scale.ScaleLinear&lt;number, number&gt; | D3Scale.ScaleTime&lt;number, number&gt;;
      switch (scaleIdentity) {
          case scaleIdentites.linear:
              scaleFunction = D3Scale.scaleLinear();
              scaleFunction.domain([1, 2]); // correctly reads the correct type and doesnt error.
              break;
          case scaleIdentites.time:
              scaleFunction = D3Scale.scaleTime();
              scaleFunction.domain([1, 2]); // correctly reads the correct type and doesnt error.
              break;
          default: {
              throw new Error(`Unknow scale ${scaleIdentity}`);
          }
      }
      if (domain) {
          scaleFunction.domain(domain); // error as saying should have 0 parameters.
      }
  };
  </code></pre>
  
  <p>When inside the <code>case</code> block it correctly allows me to use a parameter in <code>domain</code>. Outside it errors.</p>