title: Narrowing down to instanciated class given a constant association map
body: <p>How can I narrow down the class that will be instanciated using mapped types like this ?</p>
  <p><a href="https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBMAdgVwLZwOoEsAmBzYGAFQE8xg4BvAKDjgAUBJAUTgF44AiMLYTgGlpwAQgEEASuy4AjAIZQBQgMIB5AKoA5Is0kdOAYwjJEMYAsEBfavoA2sgM724SoybMAJWYhw2zlK7YOTgDiUEZgAGqyNsjA9p7evlD+1NYQiPbw7qIaACIAMjoAygD6ALKi9FI0dADa2PiEpOQAdKqa2uIAugBczq6mUAk+ZoJ1DQTEZMAtYt19oeFRMXHDSWNw9biTzTNMzL1wi8iR0bHxXiNQlnAOcIYZMADcqaCQsHAw00eEa2YAPEQECBTN4nBMmtMAHzVIS1ADScCwiEw20h5EOX3IEAAZnBsnlCuJShV6Aiui8LLUiBTUliKMIpMFfpckv8IVNWu0tDooU84AB6AX9YyDP5QOAAWhhKnhr3A0HggUcqMaMHFZVkYHIEpqcDAyGkNiw+jgk3FgOBoJw4LRnOAUIAFEI6ABrYAkPoc3YbACUfUYjy8+mAu3+zPVrIBRChML1dCghGQUBRiGAAHd8TkCsVypVau6SF1Hb6XnQrFY0o84KgtTqpGnMxyNXWzCWq5k4NIpLXtWYWuao1BHd7pm11DzxL7BcL6biu3B7AALIw2HBdiguUUeIdAA" rel="nofollow noreferrer">Typescript playground</a></p>
  <pre class="lang-ts prettyprint-override"><code>export enum WidgetType {
    PIE = &quot;pie&quot;,
    BAR = &quot;bar&quot;,
    COUNTER = &quot;counter&quot;,
  }
  class CounterHandler{}
  class GroupValuesHandler{}
  
  const HANDLERS_MAP = {
    [WidgetType.COUNTER]: CounterHandler,
    [WidgetType.BAR]: GroupValuesHandler,
    [WidgetType.PIE]: GroupValuesHandler,
  } as const;
  
  export type GetHandler&lt;T extends WidgetType&gt; = {
    [K in WidgetType]: typeof HANDLERS_MAP[K];
  }[T];
  
  type B = GetHandler&lt;WidgetType.COUNTER&gt;; // CounterHandler -&gt; OK
  
  export class WidgetHandlerMapper {
    public getHandler&lt;T extends WidgetType&gt;(
      key: WidgetType,
    ): InstanceType&lt;GetHandler&lt;T&gt;&gt; {
      return new HANDLERS_MAP[key](); // Error, narrows to CounterHandler | GroupValuesHandler
    }
  }
  
  const mapper = new WidgetHandlerMapper()
  const b = mapper.getHandler(WidgetType.COUNTER) // typeof b should be CounterHandler
  </code></pre>