title: How can I infer an object property's value in TypeScript when using mapped types?
body: <p>I'm trying to create a singleton <code>factoryLookup</code> in typescript that enforces that I have a factory defined for every value in a union type (i.e. a mapped lookup type), however I also want to be able to infer the signature (could be a value or function of arbitrary arguments) for any value that is looked up. Here is a code sample:</p>
  
  <pre><code>// Using a simple object, I can create a map between a key and an arbitrary function or value
  const factoryLookup = {
    foo: (message: string, count: number): string =&gt; message + count,
    bar: (): number =&gt; 1,
  };
  
  // TypeScript can infer function signature of the foo and bar values
  factoryLookup.foo('hello world', 3); // foo's signature: (message: string, count: number) =&gt; string
  factoryLookup.bar(); // bar's signature: () =&gt; number
  
  type FactoryTypes = 'foo' | 'bar' | 'baz';
  
  // With a mapped type, TypeScript can enforce I have *something* defined for every member I am mapping
  const typedFactoryLookup: { [key in FactoryTypes]: any } = {
    foo: (message: string, count: number) =&gt; {},
    bar: () =&gt; {},
    baz: (obj: any) =&gt; {},
  };
  
  // However, I don't know how to infer the mapped values
  typedFactoryLookup.foo; // any type
  typedFactoryLookup.bar; // any type
  typedFactoryLookup.baz; // any type
  </code></pre>
  
  <p><strong><em>I want the type safety of ensuring I have a function defined for every value in my union/enum type, but also the type inference to know what the signature of that value is.</em></strong></p>
  
  <p>Any ideas on how to solve this, or alternative approaches that achieve the same goals would be appreciated.</p>